Want to create VBA to create a button which will go to the relevant line in a linked database for a client

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hwsilver
    New Member
    • Jun 2021
    • 7

    Want to create VBA to create a button which will go to the relevant line in a linked database for a client

    I'm trying to write VBA to create a button which will go to the relevant line in a linked database for a specific client. First I need to have VBA that IDs when the client name in the report matches that in the linked table.

    This is what I have so far.
    DoCmd.OpenForm "Clients with matches Full Report",View:=a cFormsDS, ,WhereCondition "FinCoachin g Enrollment File 2"!.Client__Nam e = " & "Client with matches Full Report"!Client_ Name, ,

    When it gets to Wherecondition it says it was expecting a parameter.

    Thanks.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32633

    #2
    You have the wrong format for the call of DoCmd.OpenForm. Check with the documentation for the correct parameters to use but one thing you certainly cannot do is to mix up positional parameters with named ones. If you want to use named parameters, and I do recommend that where possible, then the format is :
    Code:
    NameOfParameter:=Value
    A WhereCondition parameter must be a string value that is correctly formatted as a SQL WHERE clause.

    Comment

    • hwsilver
      New Member
      • Jun 2021
      • 7

      #3
      THanks, NeoPa.
      Can a table and variable appear like this? "FinCoachin g Enrollment File 2"!.Client__Nam e?

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32633

        #4
        Hi there.

        No. That wouldn't be correct syntax.

        To be fair, I've very little idea what you're asking as you give no context clues, but I can say with certainty that whether your question is about SQL or VBA then the answer is definitely "No". Otherwise, you may want to consider making your comments clearer so they make sense to those of us trying to understand you.

        Comment

        • hwsilver
          New Member
          • Jun 2021
          • 7

          #5
          I want to reference a table named "FinCoachin g Enrollment File 2" that contains a variable named Client__Name. One source said to use quotes when a file name has spaces.

          What is the correct way to do this in VBA?

          Thanks.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32633

            #6
            VBA doesn't reference Table Fields directly. I say Fields because Tables don't have Variables and I expect that's what you're trying to say.

            I was rather hoping you'd do a little bit of work before replying to ensure what you ask makes sense this time, rather than leaving it up to us again to guess what it is you're trying (but not very hard) to express. All I see is a simple repetition of the same question that was so poor before. Nevertheless, for now, I will attempt to answer as well as I can and guess what you mean.

            So then, VBA is an Access (Office+) language that works with many entities in many different ways. It doesn't give access to data (Tables, Queries, Fields, etc) natively. As such your question doesn't make sense as it stands.

            You can access data within Tables by Field (Bearing in mind obviously you also need to ensure you have the correct record selected.) either by using DLookup() or by using Recordset coding in your VBA.

            Generally speaking, and this is more for SQL than VBA though they can overlap here a bit, when dealing with items whose names contain spaces and/or other characters that may otherwise be treated as delimiters you should enclose them in brackets []. Thus, when referencing the table you would use [FinCoaching Enrollment File 2].

            It is generally considered wiser not to use such characters in names but we don't always control what we have to work with so it helps to know how to handle them when forced to.
            Originally posted by HWSilver
            HWSilver:
            One source said to use quotes when a file name has spaces.
            That may be true, but I doubt they were talking about what you're asking about. I know of no situation where quotes, of either type (' or ") would be of any help in this regard at all.

            Comment

            • twinnyfo
              Recognized Expert Moderator Specialist
              • Nov 2011
              • 3653

              #7
              I would do this:

              Code:
              Dim strWhere As String
              
              strWhere = "[FinCoaching Enrollment File 2].Client_Name = " & Me.Client_Name
              
              Call DoCmd.OpenForm( _
                  FormName:="Clients with matches Full Report", _
                  View:=acFormsDS, _
                  WhereCondition:=strWhere)
              This assumes Client_Name (with only one underscore) is a Field in the referenced Table and a field available in the Form/Report from which you are calling this code.

              Hope that hepps!

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32633

                #8
                Tsk tsk Twinny. I hope you aren't assuming that Me.Client_Name takes numeric data :-(

                PS. No sneakily fixing it before claiming innocence!

                Comment

                • twinnyfo
                  Recognized Expert Moderator Specialist
                  • Nov 2011
                  • 3653

                  #9
                  True, true, NeoPa! Faulty assumption on my part, but hopefully the suggestion points OP in the right direction.

                  I should have learned my lessons by now—I’m just so accustomed to numerical values. Here’s the text version:

                  Code:
                  Dim strWhere As String
                   
                  strWhere = "[FinCoaching Enrollment File 2].Client_Name = '" & Me.Client_Name & "'"
                   
                  Call DoCmd.OpenForm( _
                      FormName:="Clients with matches Full Report", _
                      View:=acFormsDS, _
                      WhereCondition:=strWhere)
                  Still don’t know fer sher if this is what OP is looking for.

                  Thanks for the hepp!

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32633

                    #10
                    True. It's hard to answer when people can't even ask the question properly. I guess throwing out examples might help in case one of them hits what they're actually looking for. They certainly don't hurt ;-)

                    Comment

                    Working...