Dachande11
Registered User.
- Local time
- Today, 19:18
- Joined
- May 1, 2001
- Messages
- 41
Hello to anyone who can help,
I am trying to update a table from VBA using another recordsets output as the criteria to select which field will be updated. The output is also used as the criteria to select what data is used to update this field.
My objective is to update 12 columns in a table in turn (these are periods in a year). I am trying at the moment to just update 1 column and then use the loop to be able to do all of them in turn but for the moment I cannot even get that working (although it has worked before about 5 times but then stopped).
I have attached the code below and would appreciate any help as I am fairly new to recordsets in VBA.
Private Sub Update_SAC_Mar_Click()
Dim conn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim Per As Variant
On Error GoTo proc_err
Set conn = CurrentProject.Connection
Set rst = New ADODB.Recordset
rst.Open "Periods", _
conn, adOpenForwardOnly, adLockReadOnly, adCmdTableDirect
'Do Until rst.EOF
'rst.MoveNext
Per = rst!Period
conn.Execute "UPDATE SAC_All_Period INNER JOIN Sales_SAC_Period_Channel " & _
"ON SAC_All_Period.SAC = Sales_SAC_Period_Channel.SAC SET SAC_All_Period." & Per & " = [niv] " & _
"WHERE (((Sales_SAC_Period_Channel.Period)like " & Per & "));"
'Loop
rst.Close
Set rst = Nothing
proc_exit:
Exit Sub
proc_err:
MsgBox Err.Description, vbCritical
Resume proc_exit
End Sub
Thanks
Mark
I am trying to update a table from VBA using another recordsets output as the criteria to select which field will be updated. The output is also used as the criteria to select what data is used to update this field.
My objective is to update 12 columns in a table in turn (these are periods in a year). I am trying at the moment to just update 1 column and then use the loop to be able to do all of them in turn but for the moment I cannot even get that working (although it has worked before about 5 times but then stopped).
I have attached the code below and would appreciate any help as I am fairly new to recordsets in VBA.
Private Sub Update_SAC_Mar_Click()
Dim conn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim Per As Variant
On Error GoTo proc_err
Set conn = CurrentProject.Connection
Set rst = New ADODB.Recordset
rst.Open "Periods", _
conn, adOpenForwardOnly, adLockReadOnly, adCmdTableDirect
'Do Until rst.EOF
'rst.MoveNext
Per = rst!Period
conn.Execute "UPDATE SAC_All_Period INNER JOIN Sales_SAC_Period_Channel " & _
"ON SAC_All_Period.SAC = Sales_SAC_Period_Channel.SAC SET SAC_All_Period." & Per & " = [niv] " & _
"WHERE (((Sales_SAC_Period_Channel.Period)like " & Per & "));"
'Loop
rst.Close
Set rst = Nothing
proc_exit:
Exit Sub
proc_err:
MsgBox Err.Description, vbCritical
Resume proc_exit
End Sub
Thanks
Mark