Updating combo box with data for two fields

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TimWalker
    New Member
    • Dec 2010
    • 6

    #1

    Updating combo box with data for two fields

    I am looking to update a bank table using a combo box with 2 details: Bank Name and Sort Code.

    I currently have a concatenated combo box, but am I able to add new information such as BankName : BankSortCode on update with a separator such as " : " to identify the 2 separate identities, and write this information back to the Bank table?

    Any help on this would be greatly appreciated.

    Many thanks

    Tim
  • Steve Chupp
    New Member
    • Dec 2010
    • 2

    #2
    Tim,

    You can refer to multiple fields within a combo box named cbMyComboBox using cbMyComboBox.Co lumn(0) [first field], cbMyComboBox.Co lumn(1) [second field], etc.

    Comment

    • TimWalker
      New Member
      • Dec 2010
      • 6

      #3
      Hi Steve

      Thanks for this, but not 100% sure how to implement this. At present, I am just trying to put a separator in, as I do not want 2 columns in my combo box, just the one.

      At present, my code is
      Code:
      Private Sub cboBank_NotInList(NewBank As String, NewSortCode As String, strSeparator As Variant, Response As Integer)
      Dim mbxResponse As VbMsgBoxResult
      Dim strSQL As String
      
      
      mbxResponse = MsgBox("Are you sure you want to add " & NewBank & strSeparator & NewSortCode & _
      "as a new Bank?", vbQuestion + vbYesNo)
      
      If mbxResponse = vbYes Then
      
          strSQL = "INSERT INTO Bank([BankName]) " & _
          "VALUES('" & NewBank & "');"
          strSQL = "INSERT INTO Bank([BankSortCode]) " & _
          "VALUES('" & NewSortCode & "');"
          Call DoCmd.SetWarnings(False)
          Call DoCmd.RunSQL(strSQL)
          Call DoCmd.RunSQL(strSQL1)
          Call MsgBox( _
          "The new bank information has been added to the database.", vbInformation)
          
          Response = acDataErrAdded
          
      Else
          Response = acDataErrDisplay
          
      End If
      
      End Sub
      I am new to this as well, as you will probably have guessed, so any help is much appreciated!

      Thanks a lot

      Tim
      Last edited by Niheel; Dec 14 '10, 10:15 PM.

      Comment

      • Steve Chupp
        New Member
        • Dec 2010
        • 2

        #4
        Tim, I'm getting lost on what you are trying to accomplish. If I can help I'll need a bigger picture. What does your Bank table look like (fields in it) and what are you trying to allow the user to do?

        Steve

        Comment

        • mshmyob
          Recognized Expert Contributor
          • Jan 2008
          • 903

          #5
          This does not make sense.

          You have created two seperate SQL Insert queries but the 2nd one overwrites the first.

          You then reference strSQL1 later but there is no query for it.

          If your second SQL Insert query is supposed to be strSQL1 instead of strSQL it will still not make sense because you are then trying to insert 2 seperate records with different field values and ignore other fields.

          Like Steve says you need to give us a better picture of what you are trying to accomplish.

          cheers,

          Comment

          • TimWalker
            New Member
            • Dec 2010
            • 6

            #6
            Thank you for coming back to me about this, and sorry for the delay in replying.

            As an overview, I am trying to produce a combo box where, if the required value is not present, I can type it in.

            At present, I have a combo box that has Bank Name and Bank Sort Code in a single concatenated field. This contains something like the following:-

            HSBC 400000
            NatWest 600000
            RBS 160000

            I would like my users to be able to add records by typing the following:-

            HSBC;400500

            which would then put HSBC into the Bank Name field in my table Bank, and would put 400500 into the Bank Sort Code field in the table Bank, and would then update the combo box to show this value.

            I am trying not to open a form to do this, but to use a separator to split the 2 elements of the information that is input into the 2 different fields in the table.

            I need to have the sortcode separate from the Bank Name, but it is necessary to have both pieces of information in there. I am happy to hear or any other ways that I can achieve this, and once again, thank you for your assistance.

            Cheers

            Tim

            Comment

            • mshmyob
              Recognized Expert Contributor
              • Jan 2008
              • 903

              #7
              If I understand you correctly you can try code something like so in the afterupdate event of your combo box:

              Code:
              ' check if the entry in the combo box has a ";" character
              If InStr(Me.YourComboBox.Value, ";") Then
                      Dim aStrInsert
                      Dim strSQL As String
                      'split the combo box value into 2 values using the ";" as the seperator
                      aStrInsert = Split(Me.YourComboBox.Value, ";", 2)
                      ' insert a new record with the 2 split values from the combo box
                      strSQL = "INSERT INTO tblBank (BankName, BankSort) VALUES "
                      strSQL = strSQL & "('" & aStrInsert(0) & "'," & aStrInsert(1) & ");"
                      DoCmd.RunSQL strSQL
                      Me.YourComboBox.Requery
                  End If
              This assumes that no ";" characters are allowed in either your BankName or BankOrder fields.

              Your Combo box "Limit to List" property is set to "No"
              Assumes you have an autonumber Primary key in the Bank table.

              Assumes the end user put the bank name and bank order in the correct order in the combo box ie: Bank Name first, bank order second. (you really should do some kind of error checking for this)

              Change your control names, field names, table names accordingly.

              cheers,

              Comment

              • TimWalker
                New Member
                • Dec 2010
                • 6

                #8
                Many thanks for this, looks really good. Only problem that I have is that my combo box is bound to my primary key value, which is hidden, and as such I cannot change the Limit to List property to "No", unless I unhide the primary key (which is an autonumber), and then the combo box doesn't give me the values that I want displayed!

                I think I may look at getting a form to load to update the information, unless you have any other suggestions? Thanks for all of your help, it is really appreciated.

                Kind regards

                Tim

                Comment

                • mshmyob
                  Recognized Expert Contributor
                  • Jan 2008
                  • 903

                  #9
                  What do you mean it does not give you what you want. All you need to do is create a proper query for the Row Source of the combo box.

                  If you give more details I may be able to create it for you.

                  But I will say that the way you want to do it with just the combo box is not the ideal way to go. Normally you would create a proper form with proper value checking and such to ensure data entegrity.

                  cheers,

                  Comment

                  • TimWalker
                    New Member
                    • Dec 2010
                    • 6

                    #10
                    Thanks Mshmyob

                    I think that I will look at the proper form, to ensure that the data I capture is correct.

                    For your info, the Query that I have in the Row Source at present is:-

                    SELECT Bank.BankID, Bank.BankName, Bank.BankSortCo de FROM Bank ORDER BY Bank.BankName;

                    Thanks for all your help

                    Tim

                    Comment

                    • mshmyob
                      Recognized Expert Contributor
                      • Jan 2008
                      • 903

                      #11
                      Just remove the Bank.BankID column and you should be OK.

                      cheers,

                      Comment

                      • TimWalker
                        New Member
                        • Dec 2010
                        • 6

                        #12
                        Thanks mshmyob

                        All sorted and really appreciate your help.

                        Cheers

                        Tim

                        Comment

                        • mshmyob
                          Recognized Expert Contributor
                          • Jan 2008
                          • 903

                          #13
                          You're welcome. Good luck with the rest of your project.

                          cheers,

                          Comment

                          Working...