royster
Oct 22nd, 2004, 05:11 AM
I have a problem, I am trying to pass a VBA control to a function, but I cannot seem to be able to do this. The control can be a Textbox, a Checkbox or a MSHFlexgrid, so I want a generic function that allows me to do this. Depending on the control, I can use SELECT CASE to handle it within the function.
Passing the MSHFlexgrid and the Checkbox is easy, its the Textbox I am having problems with. You cannot call the Textbox by name because VBA assumes you are calling the Textbox.text field, and this causes a Type mismatch
For example I have a Textbox called Text1, a MSHFlexGrid called Grid1 and a CheckBox called Check1.
Private Sub SaveData(ctrl as Control)
Select Case ctrl.ControlType
Case 119 'MSHFlexGrid
Dim g as MSHFlexGrid
Set g = ctrl.object
'Do Grid manipulation
Case 106 'Checkbox
Dim c as Checkbox
Set c = ctrl
'Do Checkbox manipulation
Case 109 'Textbox
Dim c as Textbox
Set c = ctrl
'Do Textbox manipulation
End Select
End Sub
Private Sub MyCallingProc
Call SaveData(Grid1) 'Works fine
Call SaveData(Check1) 'Works fine
Call SaveData(Text1) 'Fails with a Type Mismatch Error
End Sub
The reason why the Call SaveData(Text1) call fails is because it VBA is assuming that I am passing the value of Text1, i.e. the text, and not the object, which is what I am trying to pass.
Is there a way around this?
P.S. I am using MSAccess 2000.
Passing the MSHFlexgrid and the Checkbox is easy, its the Textbox I am having problems with. You cannot call the Textbox by name because VBA assumes you are calling the Textbox.text field, and this causes a Type mismatch
For example I have a Textbox called Text1, a MSHFlexGrid called Grid1 and a CheckBox called Check1.
Private Sub SaveData(ctrl as Control)
Select Case ctrl.ControlType
Case 119 'MSHFlexGrid
Dim g as MSHFlexGrid
Set g = ctrl.object
'Do Grid manipulation
Case 106 'Checkbox
Dim c as Checkbox
Set c = ctrl
'Do Checkbox manipulation
Case 109 'Textbox
Dim c as Textbox
Set c = ctrl
'Do Textbox manipulation
End Select
End Sub
Private Sub MyCallingProc
Call SaveData(Grid1) 'Works fine
Call SaveData(Check1) 'Works fine
Call SaveData(Text1) 'Fails with a Type Mismatch Error
End Sub
The reason why the Call SaveData(Text1) call fails is because it VBA is assuming that I am passing the value of Text1, i.e. the text, and not the object, which is what I am trying to pass.
Is there a way around this?
P.S. I am using MSAccess 2000.