I am trying to run a simple update query which should obtain a value from a form and update an already existing table. This is done using VBA. On executing no error message is shown but the table isn't updated with the new value from NI. The code is as follows.
strsql = "UPDATE t_datefromform SET "
strsql = strsql & "[txtmonth] = '" & [Forms]![SupplierScorecard].[txtMonth] & "'"
Debug.Print strsql
CurrentDb.Execute strsql
set [txtmonth] is an input parameter
use
strsql = "Update t_datefromform set txtmonth = '" & [Forms]![SupplierScorecard].[txtMonth] & "'"
Related
I have the following query running in the background when the user press a button on the form.
'Insert new data
strSQL = "INSERT INTO TBL_DIRECTORS " & _
"SELECT importDirectors.* " & _
"FROM importDirectors LEFT JOIN TBL_DIRECTORS ON importDirectors.[CompanyRegNumber] = TBL_DIRECTORS.[CompanyRegNumber];"
DoCmd.RunSQL strSQL
'Update existent data
strSQL = "UPDATE TBL_DIRECTORS INNER JOIN importDirectors ON (importDirectors.[OperatorName] = " & _
"TBL_DIRECTORS.[OperatorName]) AND (importDirectors.CompanyRegNumber = TBL_DIRECTORS.CompanyRegNumber) SET "
For Each field In fields
strSQL = strSQL & strFormat("TBL_DIRECTORS.[{0}] = [importDirectors].[{1}], ", field.Name, field.Name)
Next
strSQL = Left(strSQL, InStrRev(strSQL, ",") - 1) & ";"
DoCmd.RunSQL strSQL
When the UPDATE query is running I know that we have this on the status bar:
It is a acSysCmdInitMeter.
Is there a way to get this and shows in other place, like on my form?
I created a saved update query as below, which has control values and IIf function.
UPDATE SYS_AAAA_AAAH
SET SYS_AAAA_AAAH.AAK = AAA & " " & AAB & IIf(IsNull(AAC),"","(" & AAC & ")") & IIf(IsNull(AAF),""," not null") & " comment '" & AAH & "',"
WHERE (((SYS_AAAA_AAAH.AAO)=[forms]![frmAdmiTabl]![CombSAAO]));
DoCmd.OpenQuery can run it while Currentdb.Execute gives an error message 'too few parameters'. I created another saved update query without input from control or function and Currentdb.Execute worked. I don't want to see the warning message from Docmd.OpenQuery and I dont want to mess around by turning on and off the warning. Anyway of getting Currentdb.Execute work on this?
When you want to update a column of some certain records with another column's value and IIf function, it is better to use DAO.recordset edit and update
Dim Rs_AAAH As DAO.Recordset
Set Rs_AAAH = CurrentDb.OpenRecordset("select * from Table where AAO='" & Me.CombSAAO.Value & "'", dbOpenDynaset)
Rs_AAAH.MoveFirst
Do Until Rs_AAAH.EOF
With Rs_AAAH
.Edit
.Fields("AAK").Value = Rs_AAAH.Fields("AAA") & " " & Rs_AAAH.Fields("AAB") & IIf(IsNull(Rs_AAAH.Fields("AAC")), "", "(" & Rs_AAAH.Fields("AAC") & ")") & IIf(IsNull(Rs_AAAH.Fields("AAF")), "", " not null") & " comment '" & Rs_AAAH.Fields("AAH") & "',"
.Update
End With
Rs_AAAH.MoveNext
Loop
Rs_AAAH.Close
Set Rs_AAAH = Nothing
I have written a VBA code in Access. But, a table or a query does not added to the access. I do not see the results of the VBA code in access.
Option Compare Database
Sub TransformX1()
Dim strSQL As String
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Set dbs = CurrentDb
strSQL = ""
strSQL = strSQL & "TRANSFORM Sum(BAR1.[TON]) AS SumOfTON " & vbCrLf
strSQL = strSQL & "SELECT BAR1.[MABD], Sum(BAR1.[TON]) AS [Total Of TON] " & vbCrLf
strSQL = strSQL & "FROM BAR1 " & vbCrLf
strSQL = strSQL & "WHERE (((BAR1.[MABD])<1300) AND ((BAR1.[MAGH])<1300) AND ((BAR1.G)=1)) " & vbCrLf
strSQL = strSQL & "GROUP BY BAR1.[MABD] " & vbCrLf
strSQL = strSQL & "PIVOT BAR1.[MAGH];"
Set rst = dbs.OpenRecordset(strSQL)
End Sub
Since your SQL is static, there is no reason to (re)create it in VBA.
Create a query and paste your SQL into this. This will be a crosstab query.
Save it using a name, say, Q1.
Now, create a new query, say Q2, where you use Q1 as source. Adjust query Q2 to be either an append query or a create table query. This query you can run (execute) at any time.
I have inherited a vba project and have been recently getting my feet wet with vba development. I have enountered an error Compile Error Object Required when running a portion of code that I have recently made edits to. Since it's a compile error, I cant see at runtime exactly which line is throwing the error. The only code I have modified is the ElseIf block in the following code... all help is appreciated.
If tableType = 1 Then 'Resident/Local
Set db = CurrentDb
db.Execute "ALTER TABLE [" & TableName & "] ALTER COLUMN [" & KeyColumn & "] COUNTER(1,1)"
ElseIf tableType = 4 Then
Set conn = New ADODB.Connection
Dim connectionString As String
Set connectionString = DLookup("[Connect]", "[MSysObjects]", "[Name] = '" & TableName & "'")
conn.connectionString = connectionString
conn.Open
Dim cmd As ABODB.Command
Set cmd = New ADODB.Command
cmd.ActiveConnection = conn
cmd.CommandText = "ALTER TABLE [" & TableName & "] ALTER COLUMN [" & KeyColumn & "] COUNTER(1,1)"
cmd.Execute
conn.Close
Else 'Linked Table
Set wsp = DBEngine.Workspaces(0)
Set db = wsp.OpenDatabase(DLookup("[Database]", "[MSysObjects]", "[Name] = '" & TableName & "'"))
db.Execute "ALTER TABLE [" & TableName & "] ALTER COLUMN [" & KeyColumn & "] COUNTER(1,1)" 'reset the autonumber column value
End If
Remove the Set from this line:
Set connectionString = DLookup("[Connect]", "[MSysObjects]", "[Name] = '" & TableName & "'")
Set is only used for assigning object references, and connectionString is a String.
For future reference, the VBE will highlight the line with the compile error if you use Debug->Compile from the menu.
Normally if I was writing and if statement o would have it like this
If tableType = 1 Then 'Resident/Local
Set db = CurrentDb
db.Execute "ALTER TABLE [" & TableName & "] ALTER COLUMN [" & KeyColumn & "] COUNTER(1,1)"
Else
If tableType = 4 Then
give that a bash. May just bee the formatting error
I tested an UPDATE query in Access's query design, and it works, but when I try to use it in my module, I get the error:
Invalid SQL statement; expected... or 'UPDATE'.
My query:
strSql = "UPDATE " & rs.Fields("tableName") & _
" SET " & rs.Fields("foreignKeyName") & " = " & rsContacts.Fields("contactId") & _
" WHERE contactId = " & ContactID
rs: a table that has tableName, foriegnKeyName of the tables to update
rsContacts: a list of contactIds (currently standing on a particular one).
The actual string comes out like this:
UPDATE myTable SET ContactId = 5 WHERE contactId = 2
If the query works, and it is an action query, why am I getting this error?
This is my full code:
Public Sub updateChildTables(ByVal ContactID As Long, ByVal CompanyID As Long)
Dim strSql As String
Dim rs As Recordset
Dim rsPending As Recordset
strSql = "SELECT contactID FROM contacts _
WHERE companyId = " & CompanyID & " and contactId <> " & ContactID
Set rs = CurrentDb.OpenRecordset(strSql)
If Not (rs.BOF And rs.EOF) Then
rs.MoveFirst
strSql = "SELECT * FROM childTables"
Set rsChild = CurrentDb.OpenRecordset(strSql)
rsChild.MoveFirst
Do While Not rsChild.EOF
strSql = "UPDATE " & rsChild.Fields("tableName") & " SET " & rsChild.Fields("foreignKeyName") & " = " & rs.Fields("contactId") & " WHERE contactId = " & ContactID
DoCmd.RunSQL strSql
rs.moveNext
Loop
rsChild.Close
Set rsChild = Nothing
End If
Here is my idea for debugging and possibly even resolving this.
Create a query from within Access normally -- name it UpdateMyTable, for the sake of this example.
Then, rather than using the DoCmd, actually execute this specific query from your VBA.
Dim qry As QueryDef
strSql = "UPDATE " & rsChild.Fields("tableName") & " SET " & _
rsChild.Fields("foreignKeyName") & " = " & _
rs.Fields("contactId") & " WHERE contactId = " & ContactID
Set qry = CurrentDb.QueryDefs("UpdateMyTable")
qry.SQL = strSql
qry.Execute
The big advantage of this is that you can very easily debug this from within Access to both see the rendered SQL and manually run it / tweak it.