Consulting

Results 1 to 10 of 10

Thread: Parameter from CommandBar

  1. #1
    VBAX Newbie
    Joined
    Sep 2005
    Posts
    3
    Location

    Parameter from CommandBar

    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

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    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.

  3. #3
    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.

    [VBA]
    Sub OpenThisForm(Name as String)
    DoCmd.OpenForm Name
    End Sub
    [/VBA]

  4. #4
    VBAX Newbie
    Joined
    Sep 2005
    Posts
    3
    Location
    I am trying to run a procedure from a button on the toolbar passing a parameter to it. Here is a snippet:

    [VBA]
    ' 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
    [/VBA]

    How does the CommonOpenForm procedure use the parameter?

    Regards
    Last edited by xCav8r; 09-28-2005 at 03:39 PM. Reason: added VBA tags

  5. #5
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    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

  6. #6
    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.

  7. #7
    VBAX Newbie
    Joined
    Sep 2005
    Posts
    3
    Location
    Thanks all. I'll investigate further. What I want to do is Norie's earlier suggestion - only in code.

    Thanks again.

  8. #8
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    hehe,

    Welcome to VBAX!

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

    [VBA] CommandBars.ActionControl.Parameter[/VBA]
    HTH!

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

  9. #9
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    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.
    [VBA]
    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[/vba]

    This is the code that allows the user to create a new query rather than show an existing one.
    [vba]
    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[/vba]
    Last edited by xCav8r; 09-28-2005 at 09:39 PM. Reason: Changed code to VBA tags

  10. #10
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    Quote Originally Posted by chocobochick
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •