Results 1 to 3 of 3
  1. #1
    dwif is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    6

    Concat textbox name with string

    Hi,

    greetings for all access experts in this forum

    I got problems when trying to concat between my text box name with string.
    In my form there are 10 textboxs named : Student1, Student2, Student3... Student10
    I tried to get value from each textbox
    This is my code :

    Dim a as string
    a = 1
    do until a = 2
    MsgBox (Me.Controls("Student"&a).Value)
    a = a+1
    loop

    It's not working. No error also.
    Any help would be appreciated.
    Many thanks.
    Last edited by dwif; 06-08-2013 at 07:34 AM.

  2. #2
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    Dubai
    Posts
    614
    the variable "a" is declared as string. the result of a = a+1 is "a + 1" and not 2. Declare it as number (integer).
    further to concat, the the values should be joined by "&". for ex. the result of Me.txtStudent.value & 1 is StudentName1

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    VBA will take a string variable and perform math calc. If the variable has only numeric characters and all the other terms of the expression are numeric then the + will sum. However, use + with 2 string variables and they will concatenate. The + is valid concatenation operator left over from ancient BASIC. The & is preferred concatenation operator in VBA but the + still works and must be careful with it. So amrut's point is still valid, properly declaring variables should be foremost consideration in writing code.

    Here is a little test that will demonstrate:

    Sub test()
    Dim a As String, b As String, i As Integer
    a = 2
    b = 3
    Debug.Print a + b 'the output is concatenation
    Debug.Print a + 1 'the output is sum and is numeric type
    For i = 1 To 5
    a = a + i
    Debug.Print a 'the output is sum and is string type
    Next
    End Sub

    So your looping code should work. It should generate only 1 MsgBox popup because of the Do Until a = 2 limitation. Why do you say it isn't? What happens - if no error message then are the results incorrect or nothing happens?

    Try alternative loop that doesn't require incrementing with +, let VBA do it for you:
    For i = 1 to 10
    MsgBox Me.("Student" & i)
    Next

    Why do you have multiple similar name textboxes? Makes me think the data structure is not normalized.

    The .Value is not needed because Value is default property.
    Last edited by June7; 06-08-2013 at 05:17 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Concat a string that already has "&" in it.
    By jscriptor09 in forum Programming
    Replies: 1
    Last Post: 02-07-2013, 01:15 PM
  2. Using an sql string as the datasource for a textbox.
    By rghollenbeck in forum Queries
    Replies: 5
    Last Post: 10-08-2011, 07:09 PM
  3. CRLF automatically appended in string concat?
    By tnt in forum Programming
    Replies: 2
    Last Post: 10-04-2011, 09:36 PM
  4. Replies: 2
    Last Post: 09-07-2011, 11:33 AM
  5. Replies: 7
    Last Post: 02-23-2011, 06:26 PM

Tags for this Thread

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