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.

VB Code:
  1. Private Sub SaveData(ctrl as Control)
  2.     Select Case ctrl.ControlType
  3.     Case 119   'MSHFlexGrid
  4.         Dim g as MSHFlexGrid
  5.         Set g = ctrl.object
  6.         'Do Grid manipulation
  7.     Case 106   'Checkbox
  8.         Dim c as Checkbox
  9.         Set c = ctrl
  10.         'Do Checkbox manipulation
  11.     Case 109   'Textbox
  12.         Dim c as Textbox
  13.         Set c = ctrl
  14.         'Do Textbox manipulation
  15.     End Select
  16. End Sub
  17.  
  18.  
  19. Private Sub MyCallingProc
  20.     Call SaveData(Grid1)        'Works fine
  21.     Call SaveData(Check1)       'Works fine
  22.     Call SaveData(Text1)        'Fails with a Type Mismatch Error
  23.  
  24. 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.