Add new entries to sql table form MS ACCESS front end.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HiGu
    New Member
    • Feb 2012
    • 99

    Add new entries to sql table form MS ACCESS front end.

    I have two tables which can be represented by this query (I have made this query the Recordsource of the form):
    Code:
    SELECT tblrcmtask.id, tblrcmtask.rcmtask,tblrcmtaskoptions.id, tblrcmtaskoptions.rcm_id, tblrcmtaskoptions.rcmtaskoptions FROM tblrcmtask INNER JOIN tblrcmtaskoptions ON tblrcmtask.id=tblrcmtaskoptions.rcm_id
    I want the user to be able to add new entries into these table via a form in access 2007.
    Columns tblrcmtask.id and tblrcmtaskoptio ns.id are the primary keys of the tables tblrcmtask and tblrcmtaskoptio ns respectively.
    I do not understand how do I create new ID in both the tables while the user adds new entries.The user can add only tblrcmtaskoptio ns.rcmtaskoptio ns and tblrcmtask.rcmt ask in the form.Also, there are multiple rows in the table tblrcmtaskoptio ns for each tblrcmtask.id.
    I want the user to be able to add new rows in the table tblrcmtaskoptio ns for an existing tblrcmtask.id

    I tried using dropdowns for these two but I am facing problem while creating the new ID as Maximum of the ID + 1.
    Here's the code:
    Code:
    Private Sub Form_Current()
    If Me.AllowAdditions = True And IsNull(Me.txtRCMTASKID) Then
    Dim MyRecords As DAO.Recordset
    Dim Myfield As DAO.Fields
    SQL = "SELECT Max(tblRCMTASK.ID) AS MaxOf_RCMTASKID FROM tblRCMTASK;"
       Set MyRecords = dbTHIS.OpenRecordset(SQL)
       Set Myfield = MyRecords.Fields
       Me.txtRCMTASKID = Myfield("MaxOf_RCMTASKID") + 1
       Me.txtRCMTASKID.DefaultValue = Myfield("MaxOf_RCMTASKID") + 1
       MyRecords.Close
    End If
    If Me.AllowAdditions = True And IsNull(Me.txtRCMOPTIONSID) Then
    Dim MyRecords1 As DAO.Recordset
    Dim Myfield1 As DAO.Fields
    SQL = "SELECT Max(tblRCMTASKOPTIONS.ID) AS MaxOf_RCMOPTIONSID FROM tblRCMTASK;"
       Set MyRecords = dbTHIS.OpenRecordset(SQL)
       Set Myfield1 = MyRecords1.Fields
       Me.txtRCMOPTIONSID = Myfield1("MaxOf_RCMOPTIONSID") + 1
       Me.txtRCMOPTIONSID.DefaultValue = Myfield("MaxOf_RCMOPTIONSID") + 1
       MyRecords1.Close
    End If
    Me.txtRCM_ID = Me.txtRCMTASKID
    End Sub
    How do I do this?
  • ariful alam
    New Member
    • Jan 2011
    • 185

    #2
    If 2nd table is the sub table of 1st table, then why you need to use primary key in the second table? if, there is no primary key in the second table, then it is possible easily to create a new id for the 1st table using access query option in a form. and add new row in the 2nd table using sub form technique for specific record in 1st table.

    Comment

    Working...