use Me. with a string variable (1 Viewer)

grahamvb

Registered User.
Local time
Yesterday, 22:22
Joined
Aug 27, 2013
Messages
57
This is probably a simple syntax question.
How do I use Me. with a variable? (Access 2010)

Code:
 Dim i as integer
 Dim myNumber as integer
 Dim strSource as string
 Dim strDestination as string
  
 For i = 11 To 16
    ' assign text box names to a strings
    strSource = "txtBoxA" & CStr(i)
    strDestination = "txtBoxB" & CStr(i) 
  
    ' convert text box string value to numeric variable
    myNumber = val(me.strSource)
 
    ' do some math
    myNumber = myNumber + 1  
    
    ' convert and display result
    me.(strDestination) = CStr(myNumber)
Thanks in advance for the assistance.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:22
Joined
Aug 30, 2003
Messages
36,126
me(strDestination)
 

grahamvb

Registered User.
Local time
Yesterday, 22:22
Joined
Aug 27, 2013
Messages
57
Thank you sir!

Code:
Dim i as integer
Dim myNumber as integer
Dim strSource as string
Dim strDestination as string
  
For i = 11 To 16
 
    ' assign text box names to a strings
    strSource = "txtBoxA" & CStr(i)
    strDestination = "txtBoxB" & CStr(i) 
  
    ' convert text box string value to numeric variable
    myNumber = val(me(strSource))

    ' do some math
    myNumber = myNumber + 1  
    
    ' convert and display result
    me(strDestination) = CStr(myNumber)
  
 Next i
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:22
Joined
Aug 30, 2003
Messages
36,126
Happy to help!
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 12:22
Joined
Jan 20, 2009
Messages
12,852
Variables in the procedure don't need to be referenced with Me. Access expects a name without a context to be a variable, first scoped to the current procedure, then to the module. Then it tries the objects.

Using Me in this context would be a distraction for most developers looking at your code. They would expect it to be referring to an object.

Your use is also inconsistent:

myNumber = val(me(strSource))

Why use Me(strSource) but just myNumber when they are both local variables?
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:22
Joined
Aug 30, 2003
Messages
36,126
The variable used with Me refers to a control name. Would it work without?
 

ChrisO

Registered User.
Local time
Today, 12:22
Joined
Apr 30, 2003
Messages
3,202
>>Would it work without?<<

No, without the use of Me the Val function simply returns the value of the string variable strSource.

The code as written is correct.

Chris.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 12:22
Joined
Jan 20, 2009
Messages
12,852
Sorry. I misunderstood what the code was supposed to be doing because I dind't read it properly.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:22
Joined
Aug 30, 2003
Messages
36,126
No worries mate; we've all done it.
 

Users who are viewing this thread

Top Bottom