Results 1 to 8 of 8
  1. #1
    joecamel9166 is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2016
    Posts
    85

    Open form with combo box value set to last record

    This was working at one point, but I had to change some things around and now it is not.
    I have a combo box(cmbCustID) where I select my customer. If the customer is already in the system, I click the name and my subform fills in automatically. The problem lies when it is a new customer, I have a button that closes frmReceive and opens frmNewCustomer.
    Once I fill in the customer info and I click the save button, this closes frmNewCustomer and opens frmReceive to last record.
    If I drop down the combo box (cmbCustID) the customer that I just entered is in there, but I want that new customer info already filled in.



    As far as I know I didn't change any of the code that would relate to these forms.

    Is there a specific code that I can put in (maybe with parameters like)
    Code:
    Private Sub Form_Load()
    If Not me.newrecord then
    Me.Customer_Number = DLast("CustNumber", "tblCustID")
    else
    end if.
    End Sub
    This code was put in frmReceive. I thought this would work, but all that happens is when I click new customer, frmNewCustomer opens to last record.

  2. #2
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    What form is your combo box on?

    I think you want to set the value of the combo box to the last customer ID, not the Customer_ID field.

    me!cmbCustID = DLast("CustNumber", "tblCustID")

  3. #3
    joecamel9166 is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2016
    Posts
    85
    cmbCustID is on frmReceive as the master field to the subform.

    this still doesn't work.

    and I need it somewhere so when I save frmNewCustomer, frmReceive opens back up with this data filled in( cmbCustID as well as all the data on the subform).

    I do not want these to be filled in when I open frmNewCustomer to new record.

  4. #4
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    What you need then is a way to tell frmReceive that it is being re-opened after a new customer was added, and that the combo box should be set to the new customer ID. You could use a Global variable for that.

    Another way to do this would be to have the code behind your new customer button just hide frmReceive, rather than closing it, open the new custome form as modal (see below), and then unhide frmReceive, something like this (untested, but close I think)

    Code:
    '
    ' Hide frmReceive
    '
    Forms!frmReceive.Visible = False
    '
    ' Open form for new customer in dialog mode, to pause THIS code until that form is closed
    '
    DoCmd.OpenForm "frmNewCustomer", , , , , acDialog
    '
    ' Unhide the frmReceive form
    '
    Forms!frmReceive.Visible = True
    '
    ' Requery the combo box to ensure new customer is included
    '
    Forms!frmReceive!cmbCustID.Requery
    '
    ' Set the combo box value to the new customer
    '
    cmbCustID = DLast("CustNumber", "tblCustID")
    This way you don't have to worry about communicating between forms quite as much.

  5. #5
    joecamel9166 is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2016
    Posts
    85
    So in the above code, I would unhide it when I was done and save frmNewCustomer.

    Just so I am clear, this is not all done under the same function, and these can be separated to work on different button clicks on separate forms?

  6. #6
    joecamel9166 is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2016
    Posts
    85
    Please disregard last post!

  7. #7
    joecamel9166 is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2016
    Posts
    85
    Changed slightly
    Code:
    me.cmbCustID.Requery
    me.cmbCustID = DLast("CustNumber", "tblCustID")
    Works Perfectly now

  8. #8
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Sorry - I posted this before I saw the disregard note. Glad you got it working. Sometimes the easiest way to pass information between forms is to keep them open.

    I would unhide it when I was done and save frmNewCustomer.
    The code would unhide frmReceive when you added a new customer and closed frmNewCustomer.
    The code would be in the On Click event of the button in frmReceive, and it pauses as long as frmNewCustomer is open.

    this is not all done under the same function, and these can be separated to work on different button clicks on separate forms?
    As I have it here, it would all be in the same On_Click event procedure of the button in frmReceive. Putting parts in different forms probably would not work because you cannot access any other forms as long as frmNewCustomer is open. That is the effect of the acDialog setting in the DoCmd.openform statement.
    Last edited by John_G; 03-30-2016 at 01:46 PM. Reason: Added first line

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 10
    Last Post: 12-17-2014, 03:31 PM
  2. Replies: 4
    Last Post: 12-16-2014, 05:08 PM
  3. Replies: 22
    Last Post: 05-10-2014, 04:43 PM
  4. Replies: 3
    Last Post: 08-26-2012, 10:04 PM
  5. Replies: 1
    Last Post: 05-03-2012, 02:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums