update Query not working from Excel 2010

amolwa

New Member
Joined
Apr 2, 2014
Messages
12
Hi,</SPAN>

i am new in VBA and Access. i am trying to develop application using forum help. your help is much appreciated.</SPAN>
i am fetching data from MS access table in excel using IPR_ID and displaying it in Userform which works fine but at the same time i would like to update AD_State for same IPR_ID with LOCKED status to lock records for others to View or Edit fetched records.</SPAN>
Once changes are updated in UserForm update IPR_ID with other changes from excel to Access with AD_State UNLOCKED . Sr No field is set as Primary Key in Database.</SPAN>

Below is the Find Records click button code in Find_Rec userform </SPAN>

Code:
Sub CommandButton1_Click()</SPAN>
    Dim cnt As ADODB.Connection</SPAN>
    Dim rst1 As ADODB.Recordset</SPAN>
    Dim wsSheet1 As Worksheet</SPAN>
   
         'Instantiate the ADO-objects.</SPAN>
        Set cnt = New ADODB.Connection</SPAN>
        Set rst1 = New ADODB.Recordset</SPAN>
        Set wbBook = ThisWorkbook</SPAN>
        Set wsSheet1 = wbBook.Worksheets(1)</SPAN>
        
         'Path to the database.</SPAN>
        stDB = "R:\Claims\MS Access Database\Claims.accdb"</SPAN>
        
         'Create the connectionstring.</SPAN>
        stConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & stDB & ";"</SPAN>
        
         'The 1st raw SQL-statement to be executed.</SPAN>
        stSQL1 = "SELECT * FROM Payables_Output where IPR_ID = '" & TextBox1 & "' And AD_State = 'UnLocked'"</SPAN>
       
        'Clear the worksheet.</SPAN>
        ThisWorkbook.Sheets(1).Range("A1").CurrentRegion.Offset(1).Clear</SPAN>
        
        With cnt</SPAN>
            .Open (stConn) 'Open the connection.</SPAN>
            .CursorLocation = adUseClient 'Necessary to disconnect the recordset.</SPAN>
        End With</SPAN>
       
        With rst1</SPAN>
            .Open stSQL1, cnt 'Create the recordset.</SPAN>
            Set .ActiveConnection = Nothing 'Disconnect the recordset.</SPAN>
        End With</SPAN>
       
        With ThisWorkbook.Sheets(1)</SPAN>
            .Cells(2, 1).CopyFromRecordset rst1 'Copy the 1st recordset.</SPAN>
        End With</SPAN>
        
         'Release objects from the memory.</SPAN>
        rst1.Close</SPAN>
        Set rst1 = Nothing</SPAN>
    '    rst2.Close</SPAN>
     '   Set rst2 = Nothing</SPAN>
        cnt.Close</SPAN>
        Set cnt = Nothing</SPAN>
       
        'update AD Status to lock records for others to view and update</SPAN>
        stSQL2 = "update Payables_output set Ad_State = 'Locked' where IPR_ID= '" & TextBox1 & "'"</SPAN>
        
            If Not Range("A2").Value = "" Then</SPAN>
                ThisWorkbook.Sheets(1).Range("A2").Select</SPAN>
                FindRec.Hide</SPAN>
                Module3.show_records</SPAN>
                Module3.label_Header</SPAN>
                Rec_Viewer.Show</SPAN>
            Else</SPAN>
                MsgBox "No Records Found for IPR " & TextBox1</SPAN>
                ThisWorkbook.Sheets(1).Range("A1").CurrentRegion.Offset(1).Clear</SPAN>
                FindRec.Show</SPAN>
            End If</SPAN>
       
End Sub</SPAN>
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
All please help me in solving this


Hi,</SPAN>

i am new in VBA and Access. i am trying to develop application using forum help. your help is much appreciated.</SPAN>
i am fetching data from MS access table in excel using IPR_ID and displaying it in Userform which works fine but at the same time i would like to update AD_State for same IPR_ID with LOCKED status to lock records for others to View or Edit fetched records.</SPAN>
Once changes are updated in UserForm update IPR_ID with other changes from excel to Access with AD_State UNLOCKED . Sr No field is set as Primary Key in Database.</SPAN>

Below is the Find Records click button code in Find_Rec userform </SPAN>

Code:
Sub CommandButton1_Click()</SPAN>
    Dim cnt As ADODB.Connection</SPAN>
    Dim rst1 As ADODB.Recordset</SPAN>
    Dim wsSheet1 As Worksheet</SPAN>
   
         'Instantiate the ADO-objects.</SPAN>
        Set cnt = New ADODB.Connection</SPAN>
        Set rst1 = New ADODB.Recordset</SPAN>
        Set wbBook = ThisWorkbook</SPAN>
        Set wsSheet1 = wbBook.Worksheets(1)</SPAN>
        
         'Path to the database.</SPAN>
        stDB = "R:\Claims\MS Access Database\Claims.accdb"</SPAN>
        
         'Create the connectionstring.</SPAN>
        stConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & stDB & ";"</SPAN>
        
         'The 1st raw SQL-statement to be executed.</SPAN>
        stSQL1 = "SELECT * FROM Payables_Output where IPR_ID = '" & TextBox1 & "' And AD_State = 'UnLocked'"</SPAN>
       
        'Clear the worksheet.</SPAN>
        ThisWorkbook.Sheets(1).Range("A1").CurrentRegion.Offset(1).Clear</SPAN>
        
        With cnt</SPAN>
            .Open (stConn) 'Open the connection.</SPAN>
            .CursorLocation = adUseClient 'Necessary to disconnect the recordset.</SPAN>
        End With</SPAN>
       
        With rst1</SPAN>
            .Open stSQL1, cnt 'Create the recordset.</SPAN>
            Set .ActiveConnection = Nothing 'Disconnect the recordset.</SPAN>
        End With</SPAN>
       
        With ThisWorkbook.Sheets(1)</SPAN>
            .Cells(2, 1).CopyFromRecordset rst1 'Copy the 1st recordset.</SPAN>
        End With</SPAN>
        
         'Release objects from the memory.</SPAN>
        rst1.Close</SPAN>
        Set rst1 = Nothing</SPAN>
    '    rst2.Close</SPAN>
     '   Set rst2 = Nothing</SPAN>
        cnt.Close</SPAN>
        Set cnt = Nothing</SPAN>
       
        'update AD Status to lock records for others to view and update</SPAN>
        stSQL2 = "update Payables_output set Ad_State = 'Locked' where IPR_ID= '" & TextBox1 & "'"</SPAN>
        
            If Not Range("A2").Value = "" Then</SPAN>
                ThisWorkbook.Sheets(1).Range("A2").Select</SPAN>
                FindRec.Hide</SPAN>
                Module3.show_records</SPAN>
                Module3.label_Header</SPAN>
                Rec_Viewer.Show</SPAN>
            Else</SPAN>
                MsgBox "No Records Found for IPR " & TextBox1</SPAN>
                ThisWorkbook.Sheets(1).Range("A1").CurrentRegion.Offset(1).Clear</SPAN>
                FindRec.Show</SPAN>
            End If</SPAN>
       
End Sub</SPAN>
 
Upvote 0

Forum statistics

Threads
1,215,355
Messages
6,124,468
Members
449,163
Latest member
kshealy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top