Setting ControlSource on textbox in report - vba

In my report why is it that when I set the ControlSource on a textbox in a report to a field name like this: txtSomeTextBox.ControlSource = [Expr1] it populates as expected but when I define a user defined function like this:
Public Function GetString(strValues As String) As String
GetString = "Values: " & strValues
End Function
Then set the ControlSource property to txtSomeTextBox.ControlSource = GetString([Expr1]) it doesn't know what to do! In fact the textbox displays: "#Type!" Why can't I pass the value of [Expr1] to the function?

You need a = to evaluate a function.
=GetString([Expr1]) as controlsource works for me.

Related

Assigning a textbox value to a multiple table/query field values

I have a Main form in formview
and a related subform in datasheet view
asking results to the same query.
In the Main form i have a "free" textbox that is not linked to any query field.
This textbox is an "input" value that i want to use for a module/function that will assign the txtbox.value to ALL the values under a query field.
Example:
Main Form:
Private Sub Command1_Click()
Dim txtbox As String
Dim qryField as String
Dim FieldName as String
Dim subfrm as Object
txtbox = textbox.Value
FieldName = "Data"
qryField = Me.RecordSource & "." & FieldName
subfrm = subfrmOfMain
Call AssignValueToAllFieldValues (subfrm, txtbox, qryField)
End Sub
In the module/function:
Public Sub AssignValueToAllFieldValues (Byval subfrm as Object, Byval txtbox as String, Byval qryField as String)
For each qryField.value in subfrm.qryField
"assign txtbox.value to qryField.value"
Next
End Sub
This doesn't work of course..
Update field in underlying table with an UPDATE action SQL. Code like:
CurrentDb.Execute "UPDATE tablename SET fieldname='" & Me.textboxname & "'".
Remove apostrophe delimiters for number type field; for date/time type use # delimiter. Use a WHERE clause if need to restrict records. Apply delimiters as needed for parameters in WHERE clause as well.

How to check in gridview that it contains any string or not from vb.net?

I have one gridview. Can I check any how that In one column if it contains any string or number. and If it contains any string than I want to do some funtion
Create a loop that goes through each item in that column, and pass that value as strValue to this handler:
If String.IsNullOrEmpty(strValue) = False Then
' Do something if it contains something
end if
Reference: https://learn.microsoft.com/en-us/dotnet/api/system.string.isnullorempty?view=netframework-4.7.2

'tx_no' is not a member of 'System.Windows.Forms.Form'

I got a error about coverting string to form object.
These code I use them to covert string to form but I want to call "quiz_back" its "tx_no" textbox but it got a error.
Any suggestion?
Dim frm As Form
Dim formName As String = "quiz_back"
formName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name & "." & formName
frm = DirectCast(Activator.CreateInstance(Type.GetType(formName)), Form)
MsgBox(frm.tx_no.Text)'error
MsgBox(quiz_back.tx_no.Text)'work
You are casting the variable back to its base type: Form, which does not contain those properties. Either remove the cast, or cast it to the correct type.

Variable Value Passing to another Form, VB.NET

I do have Two Public Variables, each are from two different forms..
Form1.VB
Public UserNo As String
Form2.VB
Public MyUserNo As String
On my Form2.VB File, I assign value to the UserNo of Form1.VB
Form1.UserNo = MyUserNo
Whenever I access the Form1.VB, the Value of the MyUserNo got empty, What should I do? Both Forms are not closed.
I also tried to re-assign the value when I need to use it on the Form1.VB
UserNo = Form2.MyUserNo
First Correct is Syntax is:
Form1.VB
Public UserNo As String
Form2.VB
Public MyUserNo As String
In Form1
UserNo=Form2.MyUserNo
Second Thing:
First you should store some value in MyUserNo before storing it into UserNo.
That's why you are getting empty value.
Make the variable static/Shared and try again,it should work.
You can have more than one instance of a form, you know. Forms are objects, just like anything else. You need a variable in each form to hold a reference to the instance of each form that you are using.
If you don't call InitializeComponent(), your complete GUI is not going to render.
...
InitializeComponent()
Form1.UserNo = MyUserNo
...
Try this:
[Form1.UserNo = form2.MyUserNo]
it work
add it to form what you want value in next form
Function getvariable()
Return (VARIABLE)
End Function
In next form to get VARIABLE
VARIABLE2 = Form.getvariable()
Use variable value as Public
For Example
In Form1:
Public Str1 as String
So in Form2 you can use:
Str2=Form1.Str1
'In frmMain i Start frmBase
Dim f As New frmBase
f.Text = "New caption for frmBase"
f.ShowDialog(Me)
'in frmBase i read and write a textbox
Dim str As String = CType(Me.Owner, frmMain).txtRicetta.Text
Console.WriteLine(str)
CType(Me.Owner, frmMain).txtRicetta.Text = "12345"
Console.WriteLine(CType(Me.Owner, frmMain).txtRicetta.Text)
What you need to do is create your variables in a module as private, then generate some assessors for them.
Example:
Module modVariables
Private strUserNoSTR as String = New String(String.Empty)
Public Property getUserNoSTR() As String
Get
Return strUserNoSTR
End Get
Set(ByVal strUserNo As String)
strUserNoSTR = strUserNo
End Set
End Property
Private strMyUserNoSTR As String = New String(String.Empty)
Public Property getMyUserNoSTR As String
Get
Return strMyUserNoSTR
End Get
Set(ByVal strMyUserNo As String)
strMyUserNoSTR = strMyUserNo
End Set
End Property
End Module
After you generate the getter and setter public methods you can notice that your two private variables are within them, when you create the variables they are accessible from any form.
The reason why you keep losing the variables value is because when you try to access its value from another form (basically you're calling it from another class) the compiler has to create a new instance of that variable and when that happened the variable is set back to its original value which is of type empty string. Calling them from a module keeps them from being re-instantiated.
How To Use Them:
To get the value of strMyUserNo you call the getter of strMyUserNoSTR:
TextBox.Text = getMyUserNoSTR
To set the value of strMyUserNoSTR:
getMyUserNoSTR = someValuePlacedInThisVariable 'This sets it's value.
TextBox.Text = getMyUserNoSTR 'Now it's value is someValuePlacedInThisVariable.

Problem with Write Custom Function in reportViewer repot

I want to use of a custom function in reportviewer's expression report .
I wrote this function in code tab section in report's property.
I want to use of this function in my feild's expression but when i write "Code" and put after that "." , intellisense in report's expression does not show me my function.
Public Function FarsiNumber(ByVal str As String) As String
Dim s As String = ""
Dim ch() As Char = str.ToCharArray
For Each c In ch
If IsNumeric(c) Then
s += CChar(ChrW(1728 + CInt(AscW(c))))
Else
s += c.ToString
End If
Next
Return s
End Function
any body can show me an example.
thanks
The Intellisense doesn't catch my custom functions either. But you can still use them.
In your case, you can use Code.FarsiNumber() in your expressions in your report