Home
Sunday, November 19, 2017
8:31:20 AM
Users online: 0   You are here >> Home > Programming

Forums | Programming Forums search
Forum FAQ
   
  1  
MS Access 2003 command buttons Help...
JPL 
25/8/08 1:59:49 PM
Apprentice

on my form i have 12 command buttons & 1 text box. what i'm trying to do is once i click a command button, have info pop up in the text box. each of the 12 buttons has a relating datasheet in Tables. Can this be achieved?

-----
Kenobi

kikz 
25/8/08 4:35:57 PM
Immortal

Yes

-----
Q6600 | 4Gb PC6400 | 2 x 500Gb RAID 0 + 2 x 320 Gb RAID 0 | 19" Benq FP591 + 24" Samsung 245B + 19" Dell | 8800GTS 640 Mb + 8400GS 256 Mb | Gigabyte GA-P35-DS3P | Antec P182 | Corsair HX-620 | Thermalright 120 Extreme | Vista x64

pappes 
25/8/08 8:56:10 PM
Titan

[forms]![formname].form![controlname]="xxx"

-----
I am petitioning to be the first person to be awarded a Nobel prize for prescience. It doesn't exist yet but mark my words it will.

JPL 
26/8/08 9:04:18 AM
Apprentice

thanks for the info Pappes, but i forgot to mention i haven't done much programing except for the odd fooling around with excel. i tried what you suggested and i get an error msg which says it can't find the form. i guess i'm doing something wrong, no doubt.

-----
Kenobi

kikz 
26/8/08 2:52:33 PM
Immortal

Lets say you have the following in your database:
- A Text Box named TextBox1
- 12 Command buttons named Command1 through Command12.
- 12 Tables named Table1 through Table12. Each table has only one record
- A field named Field1 in each of the 12 tables.

Every command button would want to extract information from the corresponding table, using the DLookup command.


Edited by kikz: 26/8/2008 04:19:36 PM

-----
Q6600 | 4Gb PC6400 | 2 x 500Gb RAID 0 + 2 x 320 Gb RAID 0 | 19" Benq FP591 + 24" Samsung 245B + 19" Dell | 8800GTS 640 Mb + 8400GS 256 Mb | Gigabyte GA-P35-DS3P | Antec P182 | Corsair HX-620 | Thermalright 120 Extreme | Vista x64

kikz 
26/8/08 4:18:41 PM
Immortal

If you want to get a little more tricky control what goes on behind the collection of buttons, given a lack of Control Arrays in Access, you can create a wrapper class for your functionality and Command Button.

The class would look like this
 
Option Explicit
Option Compare Database

Private txt As TextBox
Private WithEvents btn As CommandButton ' It is important to declare this WithEvents
' to be able to capture the Click event

Private sFieldName As String
Private sTableName As String

' Properties to set the Text Box the button click will output it's results to
Public Property Get TextBox()
Set TextBox = txt
End Property

Public Property Set TextBox(txtBox As TextBox)
Set txt = txtBox
End Property



' The button you're wrapping. Acccess can't subclass controls, so using a wrapper is the next best thing
Public Property Get Button()
Set Button = btn
End Property

Public Property Set Button(cb As CommandButton)
Set btn = cb
End Property


' The name of the field you want the data from for this button
Public Property Get FieldName() As String
FieldName = sFieldName
End Property

Public Property Let FieldName(fld As String)
sFieldName = fld
End Property

' The table you want to talk to
Public Property Get TableName() As String
TableName = sTableName
End Property

Public Property Let TableName(tbl As String)
sTableName = tbl
End Property


' Generic Action you want all your buttons to perform.
Private Sub btn_Click()
txt = DLookup(sFieldName, sTableName)
End Sub


When you create your buttons on your form, set the Tag Property to be the table you want to retrieve the record from for that button. ie "Table1".

You need to create a form level variable to store all the wrapper objects.
 
Private myButtons() As clsMyButton


Next you wrap each button on your form in the load event.
 
Private Sub Form_Load()
Dim ctrl As Control
Dim myButton As clsMyButton

ReDim myButtons(0) As clsMyButton

For Each ctrl In Me.Controls
If TypeOf ctrl Is CommandButton Then
Dim cmd As CommandButton
Set cmd = ctrl

Set myButton = New clsMyButton

ReDim Preserve myButtons(UBound(myButtons) + 1)
Set myButtons(UBound(myButtons)) = myButton

Set myButton.TextBox = TextBox1
Set myButton.Button = cmd
myButton.FieldName = "Field1"
myButton.TableName = cmd.Tag

End If
Next ctrl

End Sub

In the above code, you could just as easily access Tag property in the class we btn_Click() method in the class up the top, rather than using another Property. Using another property makes it a little more re-useable.

Now, all that's left is to create the Click handlers. If you don't do this, the wrapper class click event won't fire.
 
Private Sub Command1_Click()

End Sub

Private Sub Command2_Click()

End Sub

Private Sub Command3_Click()

End Sub

Private Sub Command4_Click()

End Sub

' And so on, for every Command Button on your Form.

-----
Q6600 | 4Gb PC6400 | 2 x 500Gb RAID 0 + 2 x 320 Gb RAID 0 | 19" Benq FP591 + 24" Samsung 245B + 19" Dell | 8800GTS 640 Mb + 8400GS 256 Mb | Gigabyte GA-P35-DS3P | Antec P182 | Corsair HX-620 | Thermalright 120 Extreme | Vista x64

  1  
Forums | Programming