Return the Value in a Textbox Loop

bakerld

Registered User.
Local time
Today, 15:00
Joined
Nov 2, 2007
Messages
14
Hello everyone!

I have the following code that loops through controls on my form to find out who the 'approver' is. It is missing one link that I cannot seem to get right. I need to find out if the textbox's VALUE is null or not. The 'txt' variable contains the control name, but I'm need to know HOW to return its VALUE in a variable. What property am I missing here? How can I return the VALUE of 'txt' in a string (let's call that MyString).I've done this before, but can't remember how/when I did it. I also searched the forum because I've seen this issue posted before, but my search text wasn't turning up what I wanted.

Thanks in advance for any help offered.

Code:
For Each ctl In Me.Controls
    If TypeOf ctl Is TextBox Then
        txt = ctl.Name
        If IsNull(txt) = False Then
            ctl.Visible = True
            If Left(txt, 3) = "App" Then
                i = Right(txt, 2)
            End If
        Else
            ctl.Visible = False
        End If
    End If
Next ctl
 
How about posting the entire SubRoutine/Function so we can see where you declare your variables and get a better understanding of your code and how it is currently working.
 
Ahhh, that would help :)

Dim ctl As Control
Dim txt As String
Dim i As Integer
Dim msg As String


Code:
For Each ctl In Me.Controls
    If TypeOf ctl Is TextBox Then
        txt = ctl.Name
        If IsNull(txt) = False Then
            ctl.Visible = True
            If Left(txt, 3) = "App" Then
                i = Right(txt, 2)
            End If
        Else
            ctl.Visible = False
        End If
    End If
Next ctl

If i = 1 Then
    msg = "You are the first Approver.  "
Else
    msg = "You are Approver # " & i & ".  "
End If

MsgBox msg, vbInformation, "SMARTForm"
End Sub
 
Start your code at Private... so we can see how it all begins, please. To where do you want to return the value of the control?
 
Code

I want to return the value that is in the text box to 'MyString'. I am assuming the line 'If IsNull(ctl.Value)-false then' is where I've gone wrong. I want to evaluate the value that is contained in the text box on that line to do the rest after it.


Code:
Private Sub Form_Load()
Dim ctl As Control
Dim txt As String
Dim i As Integer
Dim msg As String

For Each ctl In Forms(frm).Controls
    If TypeOf ctl Is TextBox Then
        If IsNull(ctl.Value) = False Then  '<----- this is where I want MyString
            ctl.Visible = True
            txt = ctl.Name
            If Left(txt, 3) = "App" Then
                i = Right(txt, 2)
            End If
        Else
            ctl.Visible = False
        End If
    End If
Next ctl

If i = 1 Then
    msg = "You are the first Approver.  "
Else
    msg = "You are Approver # " & i & ".  "
End If

MsgBox msg, vbInformation, "SMARTForm"
End Sub
 
It is easy to do but where is 'MyString' defined and what is it?
 
I didn't define it yet in the actual code. I just wanted to give it a name for now. See code below with MyString declared.

MyString needs to be set somewhere to equal the value in the text box - where/how do I actually set the value of MyString?

Thanks again!

Private Sub Form_Load()
Dim ctl As Control
Dim txt As String
Dim i As Integer
Dim msg As String
Dim MyString as string

For Each ctl In Forms(frm).Controls
If TypeOf ctl Is TextBox Then
MyString = [WHAT GOES HERE?]
If IsNull(MyString) = False Then '<----- this is where I want MyString
ctl.Visible = True
txt = ctl.Name
If Left(txt, 3) = "App" Then
i = Right(txt, 2)
End If
Else
ctl.Visible = False
End If
End If
Next ctl

If i = 1 Then
msg = "You are the first Approver. "
Else
msg = "You are Approver # " & i & ". "
End If

MsgBox msg, vbInformation, "SMARTForm"
End Sub
 
I'm afraid your code does not make a lot of sense to me but I made
some changes anyway. See if it is closer to what you want.
Code:
Private Sub Form_Load()

'-- You keep changing this code before we get something that works.
'-- Your code will keep looking at each TextBox on your form and
'-- any that are not Null will overwrite the MyString variable.

   Dim ctl As Control
   Dim txt As String
   Dim i As Integer
   Dim msg As String
   Dim MyString As String

   For Each ctl In Me.Controls
      If ctl.ControlType = acTextBox Then
         If Not IsNull(ctl) Then
            MyString = ctl.Value
            ctl.Visible = True
            txt = ctl.Name
            If Left(txt, 3) = "App" Then
               i = Right(txt, 2)
            End If
         Else
            ctl.Visible = False
         End If
      End If
   Next ctl

   If i = 1 Then
      msg = "You are the first Approver. "
   Else
      msg = "You are Approver # " & i & ". "
   End If

   MsgBox msg, vbInformation, "SMARTForm"
End Sub
 
I appreciate your time and don't mean to be rude, but you kind of offended me. I did not ask you to tell me if my code makes sense to you. I needed one simple line of code to return the value of the text box, within the loop. 'My String = ctl.Value' is the very first thing I tried in this code and it does not work. That's why I came here to ask.

You say the code keeps changing before we get something that 'works'. I have, in fact modified it several times today to refine it to do what I want it to do. That doesn't even matter though because all I wanted is how to assign the value of a text box to a variable, regardless of where it is done in the code... plain and simple. Where I finally put that line has yet to be determined. I just need to know what will do the trick - and MyString=ctl.Value isn't it because the code errors/breaks there. I also fully understand that MyString will be overwritten each time it is assigned where it is located right now, in fact that's what I want it to do at this point.

So again, I apologize for being forward with you, but you may wish to refrain from offering your personal opinions/evaluations of people's code. We don't come here for that, we come here for help from other friendly programmers.

Peace, RuralGuy and best wishes to you. :)
 
I'm sorry you were offended but it is difficult, if not impossible for me to offer suggestions if I do not understand the code or the request. Good luck with your quest.
 

Users who are viewing this thread

Back
Top Bottom