Click here to Skip to main content
15,905,781 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I Write my self This Code it's not perfect but it do what I need to get

In result I getting a data place to two tables one tblLeave and second tblLeaveDate
First just recording a Leave by Employee Second its just a dates of the leave separated on separate day to make sure I can Track and also show in what they the leave appear ect

The problem is most of the time I need to update the dates of the Leave as ppl not coming back in the date added to the data base most of the time they just coming back later

I was thinking to use something like appended Query that's mean I will just create a Form from tblLeave and add a Sub Form from tblLeaveDate and I add a VBA Code what will append new dates to tblLeaveDates using ID of the Leave from tblLeave

But I don't relay know how to create Append Query by VBA any one can help and show me correct way to do that ??

I know I can create Query buy wizard then just assign it do the button but I want t learn VBA and ADO


VB
Private Sub addRecord_Click()
  
    Dim db As Database
    Dim rs As Recordset
    Dim rs2 As Recordset
    Dim i As Integer
    Dim X As Integer
  
    'Connections
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("tblLeave")
    Set rs2 = db.OpenRecordset("tblLeaveDate")
    
    
    'Recognizing the ID
    
    X = db.OpenRecordset("SELECT @@Identity")(0)
    
      
     rs.AddNew
            
   'Checking if The Required fields was Full in
   
    If Shift = "" Then
    
        MsgBox "Employee Is Required"
        
    ElseIf txtEmployeeNr = "" Then
     
        MsgBox "Employee Is Required"
        
    ElseIf txtReasonType = "" Then
     
        MsgBox "Reason Type Is Required"
        
     Else
     'Checking for 0 As sometime @@Identity can place 0
        
    If X = 0 Then
    
       X = LastID + 1
    
    End If
   
        rs!LID = X
        rs!EmployeeNr = txtEmployeeNr
        rs!ReasonType = txtReasonType
        rs!Details = txtDetails
        rs!RTWCompleted = cheRTW
        rs!StartDate = txtStartDate
        rs!EndDate = DateofRTW
        rs!AddedBy = [Forms]![frmEmployeeList]![cboUser].Column(1)
        
        rs.Update
        
    For i = 0 To txtDays
        
    
        rs2.AddNew
        
        rs2!LID = X
        rs2!CalendarDate = txtStartDate + i
        
        
        rs2.Update
        
    Next i
        
                
    ' Clean Up and Close
    rs.Close
    rs2.Close
    
    Set rs = Nothing
    Set rs2 = Nothing
    Set db = Nothing
    
    MsgBox "The Adding Process was Successful"
Posted

1 solution

The easiest way to run any "action" query in VBA is to use DoCmd.RunSQL like this:

Dim sSQL as string

sSQL = "INSERT INTO myTable(Field1, Field2) VALUES(" & myVar1 & ", " & myVar2 & ")"
DoCmd.RunSQL sSQL

Obviously write the appropriate Access SQL query!

Please note that the code you posted does not use ADO, it uses DAO (Jet engine data object model). For years MS told us to move to ADO, but in recent releases of Access they've been nudging us back to DAO. I'd also recommend supplying other arguments to "OpenRecordset" as by default you get a Dynaset with a dynamic cursor. If you are read only, scrolling forward only you will get better performance by using a forward only Snapshot.
 
Share this answer
 
Comments
SebSCO 14-Aug-14 18:55pm    
Thank you it help and i get more read abut this approche and it make it easyer for me :)
I'm New to this but i take all advice on board and I will use them Thanx Again :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900