PDA

View Full Version : Parameter from CommandBar



hehe
09-27-2005, 04:00 PM
Hi,

I am trying to implement a sort of switchboard via a commandbar. I would like to write a common routine to open a form, but how do I pass the formname (as parameter) to this routine?

TIA,
Adri Genis

Norie
09-28-2005, 04:39 AM
Why not just right click on the main toolbar, select Customize..., on the toolbars tab select New..., then from the All Forms category on the Commands tab drag the forms onto the new toolbar.

chocobochick
09-28-2005, 08:02 AM
I'm not sure if I understand the question correctly. You just want to pass the name of a form as a parameter to a Sub or Function procedure? All you need is a String.


Sub OpenThisForm(Name as String)
DoCmd.OpenForm Name
End Sub

hehe
09-28-2005, 09:35 AM
I am trying to run a procedure from a button on the toolbar passing a parameter to it. Here is a snippet:


' Create a button with an image on the bar and set some
' properties.
Set CBarCtl = CBar.Controls.Add(Type:=msoControlButton)
With CBarCtl
.FaceId = 1000
.Caption = "FormName"
.ToolTipText = "Open FormName"
.OnAction = "CommonOpenForm"
.Parameter = "FormName"
End With


How does the CommonOpenForm procedure use the parameter?

Regards

Norie
09-28-2005, 09:53 AM
Rather than passing a value, get the value from the combobox.

I use something like this wher cboQueries is the tag of the combobox on the command bar.


qryName = CommandBars.FindControl(msoControlComboBox, , "cboQueries").Text

chocobochick
09-28-2005, 11:02 AM
I might be mistaken, but I don't think you can procedures that take a parameter can be assigned to buttons on command bars. These buttons can only trigger macros, and only a Sub procedure with no parameters qualifies as a valid macro.

If that's the case, then the only way to pass a parameter to a single procedure is to have a single object that changes states depending upon the form to be opened. A Combo Box fits the bill here. Buttons, on the other hand, are a group of several objects independant of one another, and you'd have to point each one to a different procedure.

If there was a built-in function to obtain a reference to the object that called a procedure from within the procedure, this could potentially be made to work. Unfortunately, I can't yet find such a function.

hehe
09-28-2005, 02:00 PM
Thanks all. I'll investigate further. What I want to do is Norie's earlier suggestion - only in code.

Thanks again.

xCav8r
09-28-2005, 03:35 PM
hehe,

Welcome to VBAX! :hi:

To get a parameter from a command bar, use this:

CommandBars.ActionControl.Parameter
HTH!

PS. I edited the title of the thread to make it more descriptive.

Norie
09-28-2005, 04:19 PM
hehe

What I posted was code.

It basically finds the combobox with the tag cboQueries and returns the value of that combobox.

That value could then be used to do whatever you want.

I use it do launch queries rather than forms, but the syntax is basically the same.

Function GotoQuery(qryView As Integer)
Dim qryName As String
qryName = CommandBars.FindControl(msoControlComboBox, , "cboQueries").Text
If qryName <> "New Query" Then
DoCmd.OpenQuery qryName, qryView
Else
NewQuery
End If
End Function

This is the code that allows the user to create a new query rather than show an existing one.

Public Function NewQuery() As Boolean
' this will create then open a new query called qryName
On Error GoTo Err_NewQuery
Dim qryName
Dim db As Database
qryName = InputBox("Please enter a name for the query:", "Create new query")
If qryName = "" Then
Exit Function

Else

Set db = CurrentDb
db.CreateQueryDef qryName
DoCmd.OpenQuery qryName, acViewDesign

End If
NewQuery = True

Exit_NewQuery:
Exit Function
Err_NewQuery:
MsgBox Err.Description, vbInformation, "Error message"
NewQuery = False
Resume Exit_NewQuery

End Function

xCav8r
10-10-2005, 10:10 AM
I might be mistaken, but I don't think you can procedures that take a parameter can be assigned to buttons on command bars. These buttons can only trigger macros, and only a Sub procedure with no parameters qualifies as a valid macro.

I haven't tried assigning procedures with non-optional arguments to buttons on command bars, but I usually create a dummy optional argument in Word and Excel to hide the procedures designed for command bar buttons from users who hit ALT+F8. Buttons on command bars work just fine with these procedures, but if you want to use parameters, you can't use them as arguments. Instead, you have to store the values in the parameter and/or tag property of the button. I mention the tag property here, because the maximum length of a string in the parameter property is 255, so it's often necessary to spill over into the tag property when your parameters contain long strings.