Results 1 to 2 of 2
  1. #1
    help is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Feb 2011
    Posts
    1

    Variable stored as a string instead of an expression

    Hello,

    I was wondering if you are able to embed a variable within a function within a query using a value based on a form.

    I.E. I have a form that has a number which changes depending on a button you press named [var]. I have text boxes in different tabs called Switch1, Switch2, Switch3, Switch4, etc. The [var] number changes to the number corresponding with the tab that is selected, e.g. tab 4, changes var to 4

    I would then like to reference the text boxes based on which number is following the word Switch. For instance, I would like to find the value for what is in the textbox Switch3. So I would like to find the value of "Switch"&[var], but it currently only stores it as a string.

    I have one value as;
    SwitchNumber: "[Forms]![Formname]![Switch"&[var]&"]"

    What I now need is the value for "interface "&[SwitchNumber], but all that shows up is "interface [Forms]![Formname]![Switch2]", instead of the value in the Switch2 form.



    Any help would be greatly appreciated.

  2. #2
    maximus's Avatar
    maximus is offline Expert
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    This is what I have done I have a form with the following controls SW1, SW2, SW3, SW4. I have another text Box Text5 default value set to 0 the value is incremented every time I click on a command button. I Have used the value of the Text5 to generate the ControlName and then Display the value of the text box using msgbox.


    Dim ctrl As Control
    Dim frm as Form
    Dim strMyControlName As String
    Dim strControlName As String

    If Me.Text5 = 4 Then
    Me.Text5 = 1
    Else
    Me.Text5 = Me.Text5 + 1
    End If


    strControlName = "Forms!Form2!SW" & Me.Text5
    Set frm = Forms("Form2")
    strMyControlName = Eval(strControlName & ".name")
    Set ctrl = frm(strMyControlName)
    MsgBox ctrl.Value

    Hope this helps.
    The above code is attached to the Onclick event of a Command Button.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Help with date stored as string
    By weisslakeguy in forum Queries
    Replies: 8
    Last Post: 05-26-2010, 11:14 AM
  2. Using a string variable to specify a control
    By Gerry in forum Programming
    Replies: 3
    Last Post: 04-14-2010, 02:28 PM
  3. Passing variable values to Stored Procedure
    By rodrigopcnet in forum Access
    Replies: 1
    Last Post: 04-14-2010, 10:35 AM
  4. Replies: 1
    Last Post: 04-13-2010, 12:18 PM
  5. Replies: 0
    Last Post: 12-05-2005, 04:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums