ms access command button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • questionit
    Contributor
    • Feb 2007
    • 553

    ms access command button

    Hi
    I am new in Ms Access

    I need to know how can i create a command button on a form. When it is clicked, a table is opened that is associated to the data in a field i have.

    E.g: if ( surName == "abc") then open table Table_Abc
    if ( surName == "qaz") then open table Table_Qaz

    i will have a lot of surnames, so tables needs to be opened accordingly.

    Will it be done by macro, or expressin builder? If it needs VB code, can you give me that as i dont know VB

    thank you very much

    hassan
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Do you need a seperate table for each surname? It may be easier to create a query that filters for the surname and then open the query. To open a query you use the code:
    Code:
    DoCmd.OpenQuery "Name of Query"

    Comment

    • questionit
      Contributor
      • Feb 2007
      • 553

      #3
      Originally posted by Rabbit
      Do you need a seperate table for each surname? It may be easier to create a query that filters for the surname and then open the query. To open a query you use the code:
      Code:
      DoCmd.OpenQuery "Name of Query"
      yes i need seperate tables.. how can i do it then?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Originally posted by questionit
        yes i need seperate tables.. how can i do it then?
        Why do you need seperate tables?

        The code to open a table for your naming convention would be:
        Code:
        DoCmd.OpenTable "Table_" & Surname

        Comment

        • questionit
          Contributor
          • Feb 2007
          • 553

          #5
          Originally posted by Rabbit
          Why do you need seperate tables?

          The code to open a table for your naming convention would be:
          Code:
          DoCmd.OpenTable "Table_" & Surname
          What will keyword Table_ do?

          will i have to re-write this line of code for every surname?

          i have different tables that contains details (more than 5 rows) of every surname.
          is there any better way doing it than having seperate tables?

          thanks a lot

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Originally posted by questionit
            What will keyword Table_ do?

            will i have to re-write this line of code for every surname?

            i have different tables that contains details (more than 5 rows) of every surname.
            is there any better way doing it than having seperate tables?

            thanks a lot
            "Table_" is not a keyword. I assumed you used the format Table_Surname to name all your tables. "Table_" & Me.Surname.Valu e will return the string Table_Surname assuming that Surname is a control on your form.

            What details of the surname are you storing? One way to keep all the data centralized is to have a field that designates which surname the record belongs to.

            Take a look at Normalisation and Table Structures, it will help you structure your tables.

            Comment

            • questionit
              Contributor
              • Feb 2007
              • 553

              #7
              Originally posted by Rabbit
              "Table_" is not a keyword. I assumed you used the format Table_Surname to name all your tables. "Table_" & Me.Surname.Valu e will return the string Table_Surname assuming that Surname is a control on your form.

              What details of the surname are you storing? One way to keep all the data centralized is to have a field that designates which surname the record belongs to.

              Take a look at Normalisation and Table Structures, it will help you structure your tables.
              thanks i am getting undertanding now.
              What is Me keyword? will it be the name of the form? I have written this code and getting an error: "method or data member not found". ?

              DoCmd.OpenTable "Table_" & Me.surName_Labe l.Value

              Comment

              • questionit
                Contributor
                • Feb 2007
                • 553

                #8
                Originally posted by questionit
                thanks i am getting undertanding now.
                What is Me keyword? will it be the name of the form? I have written this code and getting an error: "method or data member not found". ?

                DoCmd.OpenTable "Table_" & Me.surName_Labe l.Value
                I thought that surname_Label was wrong. this now code. Also i changed Me to Form (the name of the form)?

                DoCmd.OpenTable "Table_" & Form.surName.Va lue

                but now when i run i get error: .. cant find object Table_surName1.

                surName1 is a table i have created. why it cant find it?

                Comment

                • MMcCarthy
                  Recognized Expert MVP
                  • Aug 2006
                  • 14387

                  #9
                  Originally posted by questionit
                  thanks i am getting undertanding now.
                  What is Me keyword? will it be the name of the form? I have written this code and getting an error: "method or data member not found". ?

                  DoCmd.OpenTable "Table_" & Me.surName_Labe l.Value
                  1. The Me. refers to the current form.
                  2. You cannot use .Value on a label, thy the following

                  Code:
                  DoCmd.OpenTable "Table_" & Me.surName_Label.Caption
                  Mary

                  Comment

                  • questionit
                    Contributor
                    • Feb 2007
                    • 553

                    #10
                    Originally posted by mmccarthy
                    1. The Me. refers to the current form.
                    2. You cannot use .Value on a label, thy the following

                    Code:
                    DoCmd.OpenTable "Table_" & Me.surName_Label.Caption
                    Mary
                    But i have tried this:

                    DoCmd.OpenTable "Table_" & Detail.surName. Value

                    surName is Text Box
                    Detail is the Detail portion where the surName TextBox is - under the Header

                    I get following error now: Method or Data Value not found?
                    Please help.

                    Comment

                    • MMcCarthy
                      Recognized Expert MVP
                      • Aug 2006
                      • 14387

                      #11
                      Originally posted by questionit
                      But i have tried this:

                      DoCmd.OpenTable "Table_" & Detail.surName. Value

                      surName is Text Box
                      Detail is the Detail portion where the surName TextBox is - under the Header

                      I get following error now: Method or Data Value not found?
                      Please help.
                      Try ...

                      Code:
                       DoCmd.OpenTable "Table_" & Me.surName

                      Comment

                      • questionit
                        Contributor
                        • Feb 2007
                        • 553

                        #12
                        Originally posted by mmccarthy
                        Try ...

                        Code:
                         DoCmd.OpenTable "Table_" & Me.surName
                        now i get a different error:

                        '.. cant find the object Table_detail1'

                        i have a surname value 'detail1' and have also a table called 'detail1', so there should be any error generated ?

                        Comment

                        • MMcCarthy
                          Recognized Expert MVP
                          • Aug 2006
                          • 14387

                          #13
                          Originally posted by questionit
                          now i get a different error:

                          '.. cant find the object Table_detail1'

                          i have a surname value 'detail1' and have also a table called 'detail1', so there should be any error generated ?
                          If the table is called 'detail1', you don't need the 'Table_' part, just ...

                          Code:
                          DoCmd.OpenTable Me.surName

                          Comment

                          • questionit
                            Contributor
                            • Feb 2007
                            • 553

                            #14
                            Originally posted by mmccarthy
                            If the table is called 'detail1', you don't need the 'Table_' part, just ...

                            Code:
                            DoCmd.OpenTable Me.surName
                            That worked. thanks

                            by the way. would you tell me why was i actually told to use 'Table_' .. when do we need this?

                            Comment

                            • MMcCarthy
                              Recognized Expert MVP
                              • Aug 2006
                              • 14387

                              #15
                              Originally posted by questionit
                              That worked. thanks

                              by the way. would you tell me why was i actually told to use 'Table_' .. when do we need this?
                              Rabbit was talking about naming conventions. By convention tables are named Table_Name or tblName.

                              Mary

                              Comment

                              Working...