Results 1 to 5 of 5
  1. #1
    cypress1976 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2016
    Posts
    3

    INSERT INTO on relational tables

    I have 3 tables:

    OFFICERS
    ID, FName, LName, DSN

    INTERNALCERT
    ID, InternalCourse, Expiration

    INTERNALCERTDATE
    ID, CertDate1, ExpDate1, FKOfficer, FKInternalCert

    I am writing a VB.net application and I am trying to figure out the INSERT query. I am needing to INSERT data into the INTERNALCERTDATE table WHERE INTERNALCERTDATE.FKOfficer = OFFICERS.ID and INTERNALCERTDATE.FKInternalCert = INTERNALCERT.ID



    I have a form and I will checkmark several officer's names (not their table ID). I will then select 1 Internal Course (InternalCourse, not ID) and I will select a certification date and an expiration date. When I click the save button, I need to run the INSERT query to put all the data into the INTERNALCERTDATE table with the referenced FK's from the other 2 tables.

    Any help would be appreciated. Hopefully I stated this clearly. Thank you!!

  2. #2
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    How are you selecting the officers? Are you using a multi-select list box, or some other arrangement?

    In either case, I don't think you can use one single INSERT statement; you will need some sort of VBA iterative process. Also, if you are selecting officer names and course names, how are you getting the ID's - it's the ID's that go in to InternalCertDate.

    We need a bit more detail, please.

  3. #3
    cypress1976 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2016
    Posts
    3
    I have a checkedlistbox being loaded with all the officers names. I then checkmark the ones I want to update with the certification and the dates. The certification is selected via a combobox. I'm trying to figure out a way to do a batch update when we have mutiple people take a certification course. Let me know if this helps answer your question; I appreciate the help.

  4. #4
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    First, include the officer_ID as a column in the checkedlistbox. It doesn't have to be visible (column width = 0). Do the same for the certification ID - again, it does not have to be visible.

    Then you will need VBA code (e.g. in a command button On_Click event) to iterate through all the entries in the Officers multiselect list box, and add a record to InternalCertDate for each officer row selected.

    Something like this (untested):

    Code:
    Sub Add_Records()
        Dim varItm As Variant
        Dim rst as recordset
        set rst=currentdb.openrecordset("InternalCertDate")
    
        For Each varItm In MyListBox.ItemsSelected
          
             '
             ' Add a record to InternalCertDate
             '
             rst.AddNew
                  rst.ID =    'If ID is autonumber, you don't need this
                  rst.CertDate1 = "#" & format(me!certdate1,"mm/dd/yyyy") & "#"
                  rst.ExpDate1 = "#" & format(me!Expdate1,"mm/dd/yyyy") & "#" 
                  rst.FKOfficer = mylistbox.itemdata(varitm)   ' For this to work, Officer ID must be the bound column of the listbox)
                  rst.FKInternalCert = lstInternalCert
               rst.Update
           Next varItm
       rst.Close
    End Sub
    As I say, this is not tested, and you will of course have to modify this to use your own control and field names, but it gives an idea of what to do (I hope!)

    Check out the VBA Help entries in Access for details on the properties of listboxes - very useful.

  5. #5
    cypress1976 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2016
    Posts
    3
    Thank you for the responses. Here is what I have so far. My form loads and I have a checkedboxitems (as well as a listview for test purposes) which displays the employees. I then have a combobox for selecting the certification to use. And lastly, I have two datetimepickers which will be used to select the date the certification was taken and the date it will expire (usually a 1 or 2 year certification).

    Code:
    Public Class frmAddCert    Dim dbProvider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
        Dim TheDatabase As String = "\Certifications.accdb;Persist Security Info=True"
        Dim MyDocumentsFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        Dim FullDatabasePath As String = MyDocumentsFolder & TheDatabase
        Dim strConnection As String = dbProvider & FullDatabasePath
        'Dim strConnection As String = dbProvider & dbSource
        Dim con As New OleDb.OleDbConnection(strConnection)
        'string to load All Employees
        Dim strAllEmployees As String = "SELECT DISTINCT OFFICERS.ID, OFFICERS.LName + ', ' + OFFICERS.FName + ' - ' + OFFICERS.DSN as FullName from OFFICERS" 'WHERE EXISTS (SELECT * FROM INTERNALCERTDATE WHERE OFFICERS.ID= INTERNALCERTDATE.FKOfficer)" ' ORDER BY LName"
        Dim strAllCerts As String = "SELECT DISTINCT INTERNALCERT.ID, INTERNALCERT.INTERNALCOURSE + ' - ' + INTERNALCERT.EXPIRATION as Certifications from INTERNALCERT"
        'Insert string to add new records to multiple employees
        Dim strInsertCert As String = "INSERT INTO INTERNALCERTDATE"
    
    
    
    
        Private Sub frmAddCert_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    
            Dim dt1 As New DataTable
            Dim adapter As New OleDb.OleDbDataAdapter
            Dim command As New OleDb.OleDbCommand
            con.Open()
            command.Connection = con
            command.CommandText = strAllEmployees
            adapter.SelectCommand = command
            adapter.Fill(dt1)
            CheckedListBox1.DisplayMember = "FullName"
            CheckedListBox1.ValueMember = "FullName"
    
    
            If dt1.Rows.Count > 0 Then
                For i As Integer = 0 To dt1.Rows.Count - 1
                    CheckedListBox1.Items.Add(CStr(dt1.Rows(i).Item(0).ToString & " | " & dt1.Rows(i).Item(1)), False)
    
    
                Next
            End If
    
    
            Dim dt2 As New DataTable
            Dim adapter1 As New OleDb.OleDbDataAdapter
            Dim command1 As New OleDb.OleDbCommand
            command1.Connection = con
            command1.CommandText = strAllCerts
            adapter1.SelectCommand = command1
            adapter1.Fill(dt2)
            cboCertifications.DisplayMember = "Certifications"
            cboCertifications.ValueMember = "Certifications"
            If dt2.Rows.Count > 0 Then
                For i As Integer = 0 To dt2.Rows.Count - 1
                    cboCertifications.Items.Add(CStr(dt2.Rows(i).Item(0)))
                Next
            End If
    
    
            Dim dt3 As New DataTable
            Dim adapter2 As New OleDb.OleDbDataAdapter
            Dim command2 As New OleDb.OleDbCommand
            command2.Connection = con
            command2.CommandText = strAllEmployees
            adapter2.SelectCommand = command2
            adapter2.Fill(dt3)
            Dim j As Integer
    
    
            If dt3.Rows.Count > 0 Then
                For i As Integer = 0 To dt3.Rows.Count - 1
                    'For Each row In dt3.Rows
                    ListView1.Items.Add(CStr(dt3.Rows(i).Item(1).ToString))
                    'Next
    
    
                Next
            End If
    con.close()
        End Sub
    
    
        Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    
    
            Dim dtInsert As New DataTable
            Dim daInsert As New OleDb.OleDbDataAdapter
            Dim command As New OleDb.OleDbCommand
            con.Open()
            command.Connection = con
            command.CommandText = strAllEmployees
            daInsert.SelectCommand = command
            daInsert.Fill(dtInsert)
    
    
            'gets # of checkbox items (not checked items)
            Dim selectedIndex As Integer = CheckedListBox1.Items.Count - 1
            MsgBox(selectedIndex)
    
    
            If (selectedIndex <> -1) Then
                'loops through all checkboxes (not just checked ones)
                For i As Integer = 0 To CheckedListBox1.Items.Count - 1
                    'MsgBox(i)
                    Dim chkstate As CheckState
                    'determines if checkbox is checked (1 is checked, 0 is not checked
                    chkstate = CheckedListBox1.GetItemCheckState(i)
                    'MsgBox(chkstate)
                    If (chkstate = CheckState.Checked) Then
    
    
                    End If
                Next
            End If
    con.close()
        End Sub
    
    
    End Class
    I know this is not a vb.net forum but I felt providing the code may assist in my question.

    So I am first checking the state of the checkboxes in the CheckedListBox1. As I iterate through them, I then need to check which value is selected for the combobox (cboCertification) and finally I need to get the values from both datetimepickers. I think I can get all that.

    My question is once I have all of that, what is my INSERT INTO statement? The connection to the db will be open at the beginning of the btnSave_click event and will remain open through the entire iteration. I believe I will have multiple embedded For Loops and then in the final one, I will input my values into the INSERT statement and write to the database. Any help with the INSERT statement would be greatly appreciated. Thank you.

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

Similar Threads

  1. Relational Tables
    By soprano in forum Access
    Replies: 3
    Last Post: 11-30-2014, 02:18 AM
  2. Problem linking two tables(relational)
    By Hairy in forum Access
    Replies: 14
    Last Post: 09-22-2012, 06:28 PM
  3. Relational tables - again
    By BarbT in forum Access
    Replies: 6
    Last Post: 10-31-2010, 10:28 AM
  4. Relational tables help
    By BarbT in forum Access
    Replies: 5
    Last Post: 10-21-2010, 09:03 AM
  5. Relating 2 Tables - Relational Problem
    By jeng in forum Database Design
    Replies: 5
    Last Post: 04-01-2010, 09: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