Reputation: 11
I am working on a project to create GUI for instrument control.
I created a userform with a couple of textboxes to adjust instrument settings.
Entries to these textboxes should be numerical.
Since textbox.value is stored as variant, I have written a sub to check if the entry is numerical.
Below is the sub I have written and how I call it in textbox change event.
Private Sub cast_Val(user_in As String, user_out As Double) 'Cast User Input'
If IsNumeric(user_in) = False Then
user_in = ""
MsgBox ("Not Numeric")
Else
user_out = CDbl(user_in)
End If
End Sub
Private Sub dwell_txt_AfterUpdate()
Call cast_Val(cont_ui.dwell_txt.Value, pulse_wid)
'Dim in_txt As String
'in_txt = cont_ui.dwell_txt.Value
'Call cast_Val(in_txt, pulse_wid)
'cont_ui.dwell_txt.Value = in_txt
End Sub
In debugging mode I checked which step values of user_in, user_out, dwell_txt.Value and pulse_wid change.
I was suspicious of the scope of my variables. Even though all of my code is written under userform scope and dwell_txt (my textbox's name) should be contained underneath it as well, I tried declaring all my procedures and variables as Public. This did not work as well.
I tried changing the value of dwell_txt inside the cast_Val procedure. With this method, I managed to clear, however this is not a reliable method since I have a couple other textboxes in my GUI that I have to use cast_Val procedure in a similar vein.
I tried declaring the parameters of cast_Val procedure as byVal and byRef. In both options, the value inside dwell_txt did not change.
Lastly, I assigned my dwell_txt.Value into a procedure level variable and fed that variable into cast_Val.
This method seems to be a good workaround. I included this method at the end of my change event code. See the commented section for reference.
I am still curious about why my first method did not work. Why can't I feed textbox.value inside a procedure like I have done with a regular variable?
Also on a somewhat unrelated topic, is there a way to change textbox input type to integer/double rather than a variant?
Upvotes: 0
Views: 1571
Reputation: 11
Thanks to all the support of Tim Williams, I have managed to fix the problem. The issue lied on my declaration of the user_in
parameter.
Earlier on I have tried String, Int, Double and Variant types. However, this resulted in program passing a copy of the textbox property and did not let me change the property itself inside the procedure. When I declared user_in
as Object type and passed cont_ui.dwell_txt
, I was able to change the cont_ui.dwell_txt.Value
Below is the working version of the code posted in the original entry.
Private Sub cast_Val(user_in As Object, user_out As Double) 'Cast User Input'
If IsNumeric(user_in.Value) = False Then
user_in.Value = CStr(user_out)
MsgBox ("Not Numeric")
Else
user_out = CDbl(user_in.Value)
End If
End Sub
Private Sub dwell_txt_AfterUpdate()
Call cast_Val(cont_ui.dwell_txt, pulse_wid)
End Sub
Once again, thanks to Tim Williams for all the help.
Upvotes: 1