I have a form which is going to simulate a map and will have >700 command buttons on it to simulate plots (which will be called A_1 through Z_255 etc). The form I will draw beforehand and put all the buttons in place, but I dont want to set the onClick (which opens the plot information form filtered for that plot) manually for each button.
The code I have so far is creating the events in code associated with the form, but it appears to be actually modifying the source, rather than just creating them a runtime, so I have put checks in the loop to see if we have already created the onClick, and also to only create it for these button types (where the button is names after the block and plot number, separated by an underscore, and not prefixed with 'btn' like any other buttons I will put on the form)
At the moment, the events for each button are being created, but the code doesnt fire at all from the form. When I put the form in design view and look at the properties, the onClick event isnt linked to a procedure.
First question is: Is this the right way to be going about this?
Second: How can I link the events to the buttons?
Here is my code so far:
Code:
Option Compare Database
Private Sub setButtonEvents()
Dim ctl As Control
Dim sManzanaLetter As String
Dim nLoteNumber As Byte
Dim lngReturn As Long
Dim mdl As Module
Set mdl = Me.Module
For Each ctl In Me.Controls
If ctl.ControlType = acCommandButton Then
'Check we have a plot button (contains an underscore in button name)
If InStr(1, ctl.Name, "_") > 0 Then
'Check to see if we have already created the onClick event
If mdl.Find(ctl.Name + "_Click", 0, 0, 0, 0) = False Then
'Parse button name to get block and plot
sManzanaLetter = Left(ctl.Name, (InStr(1, ctl.Name, "_") - 1))
nLoteNumber = Right(ctl.Name, (Len(ctl.Name) - InStr(1, ctl.Name, "_")))
' Add event procedure
lngReturn = mdl.CreateEventProc("Click", ctl.Name)
mdl.InsertLines lngReturn + 1, vbTab & "DoCmd.OpenForm ""frmLote"", , , ""Projecto = 'Peten' AND Sector = '1' AND Manzana = '" + sManzanaLetter + "' AND Lote_No = " + CStr(nLoteNumber) + ""
End If
End If
End If
Next ctl
End Sub
'THE 2 SUBS BELOW ARE BEING CREATED BY setButtonEvents()....
Private Sub A_1_Click()
DoCmd.OpenForm "frmLote", , , "Projecto = 'Peten' AND Sector = '1' AND Manzana = 'A' AND Lote_No = 1"
End Sub
Private Sub A_2_Click()
DoCmd.OpenForm "frmLote", , , "Projecto = 'Peten' AND Sector = '1' AND Manzana = 'A' AND Lote_No = 2"
End Sub
Private Sub Form_Load()
setButtonEvents
End Sub
Any help would be greatly appreciated.
TIA,
Jon