building a string into a variable then text variable as a control name 
Author Message
 building a string into a variable then text variable as a control name

I will try to explain this the best I can.
I am trying to build a string that takes a table query in Access and uses the field's .Name property to build this string to a variable.
In general, is it possible to create a string value to a variable then test the value (txtAddress.text) of the string rather than the variable itself ( strFldname = txtAddress.text)?
So, rather than the IF statement below looking like this:    If strFldname = "" (where strFldname = txtAddress.txt)
It would be this:    If txtAddress.txt = ""
The same question applies to the strFldFocus.  While the code runs it does not take the variable and treat it as txtAddress.setfocus and setfocus on txtAddress.

Here is the code:

Set dbssn = CurrentDb
    Set qdfsn = dbssn.QueryDefs("GetFields")
    qdfsn.SQL = "SELECT * FROM FieldServiceReport_All"
    Set rstsn = qdfsn.OpenRecordset()

    For Each fld In rstsn.Fields
        strFldname = "txt" & fld.Name & ".text"
        strFldFocus = "txt" & fld.Name & ".setfocus"
        If strFldname = "" Then
            MsgBox (fld.Name & " Field Must Be Filled In.")
            strFldFocus
        End If

    Next fld

I want to build a string for each text box control with the .text property then check if each text box has a value.
This does not work because the strFldname variable now has a value ( a text box control and its .text property), but it is just the string instead of the value of the control name.text.  I can do this by creating a separate If...Then statement for each text box control, but I would like to run a loop of somekind that is similiar to above.
I hope this makes sense.



Sat, 14 Feb 2004 06:56:17 GMT  
 building a string into a variable then text variable as a control name
Hi Mark,

In vb6 (Idon't know about other versions) you can use the Controls collection to address a control using the control name as a string:

Controls("Text1").Text = "xyz"
Controls("Text3").SetFocus

If you want to loop through a set of textboxes, you could also use a control array.  All textboxes would have the same name but a different value for their Index property.

For i = 0 to 15
      If Text1(i) = "" Then
                  ......
      End  If
Net

Regards,

John..............

Quote:

> I will try to explain this the best I can.
> I am trying to build a string that takes a table query in Access and uses the field's .Name property to build this string to a variable.
> In general, is it possible to create a string value to a variable then test the value (txtAddress.text) of the string rather than the variable itself ( strFldname = txtAddress.text)?
> So, rather than the IF statement below looking like this:    If strFldname = "" (where strFldname = txtAddress.txt)
> It would be this:    If txtAddress.txt = ""
> The same question applies to the strFldFocus.  While the code runs it does not take the variable and treat it as txtAddress.setfocus and setfocus on txtAddress.

> Here is the code:

> Set dbssn = CurrentDb
>     Set qdfsn = dbssn.QueryDefs("GetFields")
>     qdfsn.SQL = "SELECT * FROM FieldServiceReport_All"
>     Set rstsn = qdfsn.OpenRecordset()

>     For Each fld In rstsn.Fields
>         strFldname = "txt" & fld.Name & ".text"
>         strFldFocus = "txt" & fld.Name & ".setfocus"
>         If strFldname = "" Then
>             MsgBox (fld.Name & " Field Must Be Filled In.")
>             strFldFocus
>         End If

>     Next fld

> I want to build a string for each text box control with the .text property then check if each text box has a value.
> This does not work because the strFldname variable now has a value ( a text box control and its .text property), but it is just the string instead of the value of the control name.text.  I can do this by creating a separate If...Then statement for each text box control, but I would like to run a loop of somekind that is similiar to above.
> I hope this makes sense.



Sat, 14 Feb 2004 09:52:51 GMT  
 building a string into a variable then text variable as a control name
Hi Mark,

Hope the following code gives you an idea of what you want.

    Dim strFields()         As String
    Dim vntField            As Variant
    Dim txtBox              As TextBox

    strFields = Split("ID,FName,LName", ",")

    For Each vntField In strFields

        On Error Resume Next
        Set txtBox = Nothing
        Set txtBox = Me.Controls("txt" & CStr(vntField))
        On Error GoTo 0
        If Trim(txtBox.Text) = "" Then

            MsgBox "Entry in text box " & txtBox.Name & " is empty."

        End If

   Next

<<

HTH
-Srini.

Quote:

> I will try to explain this the best I can.
> I am trying to build a string that takes a table query in Access and uses the field's .Name property to build this string to a variable.
> In general, is it possible to create a string value to a variable then test the value (txtAddress.text) of the string rather than the variable itself ( strFldname = txtAddress.text)?
> So, rather than the IF statement below looking like this:    If strFldname = "" (where strFldname = txtAddress.txt)
> It would be this:    If txtAddress.txt = ""
> The same question applies to the strFldFocus.  While the code runs it does not take the variable and treat it as txtAddress.setfocus and setfocus on txtAddress.

> Here is the code:

> Set dbssn = CurrentDb
>     Set qdfsn = dbssn.QueryDefs("GetFields")
>     qdfsn.SQL = "SELECT * FROM FieldServiceReport_All"
>     Set rstsn = qdfsn.OpenRecordset()

>     For Each fld In rstsn.Fields
>         strFldname = "txt" & fld.Name & ".text"
>         strFldFocus = "txt" & fld.Name & ".setfocus"
>         If strFldname = "" Then
>             MsgBox (fld.Name & " Field Must Be Filled In.")
>             strFldFocus
>         End If

>     Next fld

> I want to build a string for each text box control with the .text property then check if each text box has a value.
> This does not work because the strFldname variable now has a value ( a text box control and its .text property), but it is just the string instead of the value of the control name.text.  I can do this by creating a separate If...Then statement for each text box control, but I would like to run a loop of somekind that is similiar to above.
> I hope this makes sense.



Sat, 14 Feb 2004 09:56:15 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. converting an object variable (name of a file) into a string variable

2. The value of a variable (String type) is the name of a variable

3. Building a variable by building a String??

4. how do i convert a string variable to a control variable

5. Dynamically creating a control with the name defined in a string variable

6. Setfocus with string variable as control name

7. Building Reference Variable strings

8. How do I use contents of variable as field name(db!rst!variable)

9. Variable Name to Variable value.

10. variables naming variables

11. Get variable name from variable?

12. Variable values as variable names

 

 
Powered by phpBB® Forum Software