Converting a string representation of the form element name intoan object. - MS Access

C

CES

All,
I'm trying to figure out how to assigned a variable control value to use as an abbreviation, but I've tried various incarnations of the code below and I continue to get errors.
I was hoping that someone might be able to take a look at this and tell me what I am missing. Thanks in advance. - CES

Private Sub Form_Current()
x= "Button1"
tmp = Me.Controls(x)

If tmp.Enabled = False Then
tmp.Enabled = True
tmp.Locked = False
tmp.BackStyle = 1
Else
tmp.Enabled = True
tmp.Locked = False
tmp.BackStyle = 1
End If
End Sub
 
D

Douglas J. Steele

Private Sub Form_Current()
x= "Button1"
Set tmp = Me.Controls(x)

If tmp.Enabled = False Then
tmp.Enabled = True
tmp.Locked = False
tmp.BackStyle = 1
Else
tmp.Enabled = True
tmp.Locked = False
tmp.BackStyle = 1
End If
End Sub

I'm confused as to why you'd cross-post this to VBScript. You cannot use Me.
in VBScript: that's only usable in VBA.

And if you're using VBA, you really should declare all of your variables.

Go into Tools | Options in the VB Editor and look on the Module tab. Set the
checkbox for "Require Variable Declaration" (I've never figured out why this
isn't the default!)

In all of the modules you've already created, put

Option Explicit

as the first (or second) line

The snippet above should be

Private Sub Form_Current()
Dim x As String
Dim tmp As Control

x= "Button1"
Set tmp = Me.Controls(x)

If tmp.Enabled = False Then
tmp.Enabled = True
tmp.Locked = False
tmp.BackStyle = 1
Else
tmp.Enabled = True
tmp.Locked = False
tmp.BackStyle = 1
End If
End Sub

Yes, you'll have to go into every routine and declare every variable before
your application will work, but in the end it's going to be worth it.
Seriously, requiring declarations can save you hours in trying to track down
subtle errors such as

Dim intLoop As Integer

Do While intLoop < 10

' lots of code

intLoop = inLoop + 1
Loop
 
C

CES

Tom said:
Assuming Me.Controls(x) does in fact return the desired object, you
will need to use a SET statement ...

set tmp = Me.Controls(x)

Tom Lavedas
==============
http://members.cox.net/tglbatch/wsh/

Tom,
I was able to get the following to work, however, I was wondering, there a way of passing the object directly into the function?

I've tried fnEnableDisableTxtBox(tmp As Control), and then removing the Dim & Set statements but continue to receive errors. Anyway thank you for the helped with the keyword Set. - CES


Public Function fnEnableDisableTxtBox(x As String)

Dim tmp As Control ' Remove?

Set tmp = Me.Controls(x) ' Remove?

If tmp.Enabled = False Then
tmp.Enabled = True
'tmp.Locked = False
'tmp.BackStyle = 1
Else
tmp.Enabled = False
'tmp.Locked = True
'tmp.BackStyle = 0
End If

End Function

Private Sub Form_Current()

Call fnEnableDisableTxtBox("Button1")

End Sub
 
D

Douglas J. Steele

Public Function fnEnableDisableTxtBox(tmp As Control)

If tmp.Enabled = False Then
tmp.Enabled = True
'tmp.Locked = False
'tmp.BackStyle = 1
Else
tmp.Enabled = False
'tmp.Locked = True
'tmp.BackStyle = 0
End If

End Function

Private Sub Form_Current()

Call fnEnableDisableTxtBox(Me!Button1)

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top