Suggestions |
example file
Check userinput in ActiveX controls in a Worksheet
|
1 Introduction 2.1 Method I 2.2 Method II 2.3 Method III 2.3.1 Classmodule 2.3.2 Code in the classmodule 2.3.3 Code in the worksheet 3 Technical remarks 4 Example files 5 Similar subject |
You can use ActiveX controls to guide users when they are entering data. You need a lot of code to prevent any imaginable mistake a user can make. A classmodule can reduce the amount of code. The same applies to Userforms. For a description how to do this in a userform see this page. We'll discuss this method for textboxes in a worksheet. Userinput results in a change in the text property of a Textbox. To check what the user does, you can use the textbox's 'Change' event. Every change in the textbox triggers this event. Let's take a worksheet, containing 10 textboxes. The names of these textboxes are: text1, text2.. ...text10. In the worksheet's VBA-module you can put this code to check whether all 10 textboxes contain any text. If they do, the button 'Continue' will turn visible. Private Sub text1_Change() For j=1 to 10
End SubIf Trim(OLEObjects.("Text" & j).Object.Text)="" Then Exit For
NextOLEObjects("btn_Continue").Object.Visible=j=11 Private Sub text2_Change() For j=1 to 10
End SubIf Trim(OLEObjects("Text" & j).Object.Text)="" Then Exit For
Next OLEObjects("btn_Continue").Object.Visible=j=11 Private Sub text3_Change() For j=1 to 10
End SubIf Trim(OLEObjects("Text" & j).Object.Text)="" Then Exit For
NextOLEObjects("btn_continue").Object.visible=j=11 etc. Private Sub text1_Change() A_check
End Sub Private Sub text2_Change() A_check
End SubPrivate Sub text3_Change() A_check
End SubPrivate sub A_check() For j=1 to 10
End SubIf Trim(OLEObjects("Text" & j).Object.Text)="" then Exit For
NextOLEObjects("btn_continue").Object.Visible=j=11 - put all textboxes in a 'Collection' - link the checking code (like A_check) to the -Change-event of this 'collection' Such a link can only be made using a 'WithEvents' method for a variable. That use is restricted to a classmodule. So we have to use one in this case. How does a classmodule work in this case ? You link using VBA all textboxes in the worksheet via a 'Collection' to an eventprocedure in the classmodule. The classmodule contains the code for the eventvariabele. NB. A classmodule can have several purposes, but we restrict ourselves here to the 'intercepting' of ActiveX-contol events. 2.3.2 What code does the classmodule contain ? First we give the classmodule a name.Any valid name will do, provided it doesn't interfere with any reserved name in VBA. Including an underscore is a safe method. In this case we use cl_activeXcheck. 1. A public object variable that contains the properties of a certain ActiveX control (e.g. a textbox). Because we want to intercept the event of the textboxes we need to add the instruction 'WithEvents'. Public WithEvents cl_textbox As MSForms.TextBox In the example file you'll see the events _Change en _DblClick: Private Sub cl_textbox_Change() ------- End Sub Private Sub cl_textbox_DblClick() ----- End Sub 2.3.3 What's in the worksheet's codemodule ? Declare a public new 'collection' variablePublic txt_collection as New Collection Private Sub Workbook_Open() For Each ctl in OLEObjects
End SubIf TypeName(ctl.Object)="TextBox" then
Nexttxt_collection.Add New cl_ActiveXcheck,ctl.name
End Ifset txt_collection(ctl.name).cl_textbox=ctl.Object So every textbox has it's own 'link' to the classmodule stored in the collection. txt_collection.add New cl_ActiveXcheck, ctl.name We need the instruction 'Set' because the ActiveX control is an object. set txt_collection(ctl.name).cl_textbox=ctl.Object - Since the variable cl_textbox in the classmodule is a Public variable, you can link all textboxes directly to this variabele ( no need for Property Let). - Since we added the name of the textbox to the collection as a key, we can refer to every textbox in the collection by it's name. - All example files have been made in Excel 2000. The example file containing all previous code example file For each ActiveX control a separate example file: TextBox ComboBox CheckBox OptionButton ToggleButton SpinButton ScrollBar Check userform controls using a classmodule |