Trying to update a combo box using a command button or report event

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Penfold
    New Member
    • Jan 2009
    • 24

    Trying to update a combo box using a command button or report event

    I have a form with a subform containing the combo box I want to update. I have a command button in the header of the main form that launches a report in print preview mode. I want to update the combo box from 'New' to 'Confirmed' when the report is generated. I have tried the following code in both the buttons on_click event and in the reports on_close event (security is set to trust the db)
    Code:
    Forms![Transactions]![Transaction Details Subform].[Form]![OrderStatusID]="1"
    There are no errors but it does not work.
    Can someone help.
    NB: There is a one-to-many relationship between the underlying tables of the main and sub forms so multiple instances of the combo box may appear on the form - All need to be updated.
  • DonRayner
    Recognized Expert Contributor
    • Sep 2008
    • 489

    #2
    Your syntax is slightly off.
    Code:
    Forms![Transactions]![Transaction Details Subform].[Form]![OrderStatusID]="1"
    should be

    Code:
    Forms![Transactions].Form![Transaction Details Subform]![OrderStatusID]="Confirmed"

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32634

      #3
      Can you perhaps share with us how this ComboBox is set up Richard?

      We don't really have enough info to determine what your problem may be. .RowSource, .ColumnCount, .ColumnWidths & .BoundColumn properties would be most helpful to start with.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32634

        #4
        Originally posted by DonRayner
        Your syntax is slightly off.
        Code:
        Forms![Transactions]![Transaction Details Subform].[Form]![OrderStatusID]="1"
        should be

        Code:
        Forms![Transactions].Form![Transaction Details Subform]![OrderStatusID]="Confirmed"
        I'm sorry Don. I feel you may be mistaken here.

        Check out Referring to Items on a Sub-Form. The .Form property is relative to the SubForm control.

        Comment

        • Richard Penfold
          New Member
          • Jan 2009
          • 24

          #5
          Half of the problem fixed

          Many thanks for the help so far.
          NeoPa
          The Combo Box control details are as follows:
          Control Source = Transactions.Or derStatusID
          Row Source = SELECT [Order Status].ID, [Order Status].OrderStatus FROM [Order Status];
          Format: Column Count = 2: Column Width = 0cm;2.5cm.

          All
          I have implemented the code suggested with one minor change as follows:
          Code:
          Private Sub Report_Close()
          Forms![Transactions]![Transaction Details Subform]![OrderStatusID] = 2
          End Sub
          This code updates one instance of the control on my form. There can be multiple instances however. As a simplified representation, the form looks like this...
          Code:
          - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
          Main Form
          CustomerName : OrderNumber : Date : Comments
           - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
          Sub Form
          New: Qty : OrderCode : Description  
          New: Qty : OrderCode : Description  
          New: Qty : OrderCode : Description  
          ...etc
          - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
          Where 'New' is the default value of the 'OrderStatusID' control I want to update

          When I run the code (close the report) I get this...
          Code:
          - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
          Main Form
          CustomerName : OrderNumber : Date : Comments
           - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
          Sub Form
          Confirmed  : Qty : OrderCode : Description  
          New        : Qty : OrderCode : Description  
          New        : Qty : OrderCode : Description  
          ...etc
          - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
          A customer may request many items on any one order. - I want to confirm all of them at once.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32634

            #6
            Ah. That's quite a different question then (at least a fundamentally different answer now the question is clearer).

            For this you will want instead, to update the records in the table (using some simple SQL) and then call a .Requery of the subform.

            We don't have the details of any filtering or linking that restricts the data on your subform, but you will need to design SQL to reflect the same records in your update query (SQL).

            If you need help with this then please reply including the info we would need to be able to show this for you.

            Comment

            • Richard Penfold
              New Member
              • Jan 2009
              • 24

              #7
              Thanks NeoPa. I'll give it a go myself over the weekend and let you know the outcome.

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32634

                #8
                Please do Richard.

                Good luck :)

                Comment

                • Richard Penfold
                  New Member
                  • Jan 2009
                  • 24

                  #9
                  Hi,
                  After much brain wracking I have to admit defeat. Can you tell me what's wrong with this...
                  Code:
                  Private Sub Report_Close()
                  Update [Transaction Details] Set OrderStatusID = 2
                  WHERE ((([Transaction Details].TransactionID) = [Forms]![Transactions]![Transaction Details Subform].[Form]![TransactionID]))
                  End Sub
                  I just get a syntax error.
                  I have tried various combinations of bracket types and quote marks. I also tried renaming the table to remove the space - Still no-go.
                  Last edited by NeoPa; Jun 22 '09, 12:43 PM. Reason: Please use the [CODE] tags provided.

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32634

                    #10
                    Yes. I can.

                    Fundamentally you are trying to execute SQL code as VBA code.

                    I'll have a look at it and see what I can do, but you need to create your SQL string first using VBA, then pass that string to be executed as SQL.

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32634

                      #11
                      Try this :
                      Code:
                      Private Sub Report_Close() 
                        Dim strSQL As String
                      
                        strSQL = "Update [Transaction Details] " & _
                                 "Set OrderStatusID = 2 " & _
                                 "WHERE [Transaction Details].TransactionID=" & _
                                 [Forms]![Transactions]![Transaction Details Subform]![TransactionID]
                        Call CurrentDb.Execute(strSQL)
                      End Sub

                      Comment

                      • Richard Penfold
                        New Member
                        • Jan 2009
                        • 24

                        #12
                        Hi NeoPa,
                        Your code works beautifully, many thanks.

                        I guess there is a VBA equivalent to the code I used but I did not find an example on the web. Does the "Dim nnn As String, nnn = and Call" wrapper to sql code work in all such instances?

                        Comment

                        • NeoPa
                          Recognized Expert Moderator MVP
                          • Oct 2006
                          • 32634

                          #13
                          It should work for all executable queries, yes.

                          Comment

                          Working...