Results 1 to 8 of 8
  1. #1
    Datament is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2023
    Posts
    18

    Use a form to add data to a non-related (non Source Record) table

    I have two forms one is called "Form1" with nine unbound Textboxes (txtbox1 - txtBox9) and a command buttom called "cmd_CustINFO". "Form1" is not related to "tbl_Master1" which is the record source for "Form2". When all the unbound textboxes are filled and the cmd_CustINFO is pushed in "Form1" it opens "Form2" with related record(s) matching the values of the unbound textboxes, but if no record matches the values of the unbound textbaoxes in "tbl_Master1" it triggers a Yes/No error message that asks if user would like to manually add the record.

    The event I would like to create is if user presses "YES" I need the values of the (txtbox1 - txtBox9) unbound textboxes from "Form1" to be saved to "tbl_Master1" and "Form2" to open with that saved record.
    Could somebody help me to achieve this with a code?
    I really appreciate your help!

    I have the following:
    MsgBox "No Record found. Do you want to manually add the record?"

    Dim db As Database Dim RS As Recordset Set db = CurrentDb Set RS = db.OpenRecordset("tbl_Master1")
    With RS
    .AddNew
    [txtBox1] = Me.Store.value (This is the mathing record in "tbl_Master1"
    [txtBox2] = Me.Account.value


    [txtBox3] = Me.Statement.value
    "
    "
    End With

    The above has not worked well for me. it did not add the values from the txtboxes in "Form1", so the desired "Form2" opened but with NO record.
    I am hoping to get a better recommendation on how to proceed. All assistance would be greatly appreciated.

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    If you edit or append with a recordset you have to commit the changes.
    See https://learn.microsoft.com/en-us/of...-dao-recordset

    You should always explicitly 'type' database objects as either DAO or ADO as appropriate. So
    Dim db as DAO.Recordset etc.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    ?? Why 2 forms? Have you considered a form explicitly for DataEntry along with some validation code in the Forms BeforeUpdate event to ensure all necessary values are filled and that no duplicates would be created via these values?
    Just curious since we don't really know anything about your business, nor the process/rationale for your proposed method.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    You are not referring to Form1?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    With RS
    .AddNew
    [txtBox1] = Me.Store.value
    shouldn’t this be

    With RS
    .AddNew
    !store=[txtBox1]

  6. #6
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    The process I think your describing is pretty straight forward.

    Your message box code would look something like this:


    Code:
        Select Case MsgBox("No Record found. Do you want to manually add the record?", vbYesNo, "Confirm Addition")
    
    
            Case vbYes
                SaveandOpen
                
            Case vbNo
                'do nothing or something else
                
        End Select
    The below code saves the record and opens the form

    Notice how the primary key of the new record is obtained then used in the criteria to open Form2

    Code:
    Private Sub SaveandOpen()
    
        Dim db As DAO.Database
        Dim rs  As DAO.Recordset
        Dim SQL_Select  As String
        Dim NewID As Long 'a variable to hold the PK of new record.
     
        SQL_Select = "Select * from tblMaster where MasterID = 0"     'open empty recordset
    
        Set db = CurrentDb()
        Set rs = db.OpenRecordset(SQL_Select)
    
        With rs
     
            .AddNew
            !fldA = Me.Tbx1
            !fldB = Me.Tbx2
            !fldC = Me.Tbx3
            !fldD = Me.Tbx4
            !fldE = Me.Tbx5
            NewID = rs.Fields("MasterID") 'get the primary key value of the new record
            .Update
     
        End With
     
        DoCmd.OpenForm "Form2", , , "MasterID = " & NewID   ' open form2 using the new PK.
     
    MyExit:
    
        rs.Close
        Set rs = Nothing
        Set db = Nothing
    End Sub
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  7. #7
    Datament is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2023
    Posts
    18
    Thank you for your suggestion. Question: are fldA, fldB, fldC from form1 and Tbx1, Tbx2, Tbx3 from form2?

  8. #8
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Here's an example
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

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

Similar Threads

  1. Replies: 9
    Last Post: 12-12-2023, 08:09 PM
  2. Add sum of data to a record related in another table
    By Rafegh in forum Database Design
    Replies: 6
    Last Post: 11-14-2014, 02:55 PM
  3. Replies: 3
    Last Post: 07-24-2014, 01:22 PM
  4. Replies: 7
    Last Post: 11-02-2012, 12:05 PM
  5. Replies: 7
    Last Post: 07-02-2012, 10:50 PM

Tags for this Thread

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