I found this code that allows me to combine all the tables in my Access DB into one big one:
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Set db = currentdb
For Each tdf In db.TableDefs
If Not (tdf.Name Like "MSys*" Or tdf.Name Like "~*" Or tdf.Name Like "test") Then
StrSQL = "INSERT INTO " & "test" & " " & _
"SELECT * " & _
"FROM " & tdf.Name & " ;"
DoCmd.RunSQL StrSQL
End If
It works perfectly but I now need this to combine all the tables but one (called Table1). I wanted to ask this question as a comment on that post but I don't have the rep to comment on it.
Bellow is the changes I've tried but haven't worked.
"FROM " & tdf.Name & " WHERE <> Table1;"
Related
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 a form with textboxes. I am inserting what the user enters into the textbox into a table. If the user enters an apostrophe in the textbox labeled "Me.ProjectName", I get an error. My code is:
CurrentDb.Execute "INSERT INTO Table1(ProjectNumber, Title) " & _
" VALUES('" & ProjectNumber & "','" & Me.ProjectName & "')"
You should not construct and execute dynamic SQL based on user input. You should use a parameterized query, something like:
Dim cdb As DAO.Database
Set cdb = CurrentDb
Dim qdf As DAO.QueryDef
Set qdf = cdb.CreateQueryDef("", _
"INSERT INTO Table1 (ProjectNumber, Title) VALUES (#prjnum, #title)")
qdf.Parameters("#prjnum").Value = ProjectNumber
qdf.Parameters("#title").Value = me.ProjectName
qdf.Execute
You should escape your strings possibly containing quotes by replacing a quote with 2 quotes:
Dim SQL As String
SQL = "INSERT INTO Table1(ProjectNumber, Title) " & _
" VALUES('" & ProjectNumber & "','" & Replace(Me.ProjectName, "'", "''") & "')"
CurrentDb.Execute SQL
I am trying to get my code to delete a newly created record if the user cancels. for some reason Access is not deleting the record even though the query is definitely filtering for unique IDs which exist within the table. Access is not throwing any errors.
PG_ID is the unique identifier in both tables, it is a Long Integer.
I've included a sample portion of my code below. Please help!
Dim var_PGID As String
Dim Delete_PG_Data, Delete_PG_Upld As String
Dim db As Database
Set db = CurrentDb
var_PGID = TempVars![var_PG_ID_NEW]
Delete_PG_Data = "DELETE * " & _
"FROM tbl_CapEx_Projects_Group " & _
"WHERE PG_ID=" & var_PGID
Delete_PG_Upld = "DELETE * " & _
"FROM tbl_CapEx_Projects_Group_Attachements " & _
"WHERE PG_ID=" & var_PGID
Debug.Print Delete_PG_Data
Debug.Print Delete_PG_Upld
db.Execute Delete_PG_Data, dbFailOnError
db.Execute Delete_PG_Upld, dbFailOnError
As requested I've switched msgbox to Debug.Print. Below is the debug.print output which runs correctly when placed in an access query.
It was a timing issue. I fixed it by committing the transaction then running the delete query. Thank you all for your input!
Private Sub cmd_Cancel_Click()
On Error Resume Next
DoCmd.SetWarnings False
If TempVars![var_NewRecord] = True Then
Do While Not Me.Recordset.EOF
Me.Recordset.Update
Me.Recordset.MoveNext
Loop
DBEngine.CommitTrans
Me.Recordset.Close
Dim var_PGID As String
Dim Delete_PG_Data, Delete_PG_Upld As String
Dim db As Database
Set db = CurrentDb
var_PGID = TempVars![var_PG_ID_NEW]
Delete_PG_Data = "DELETE * " & _
"FROM tbl_CapEx_Projects_Group " & _
"WHERE PG_ID=" & var_PGID
Delete_PG_Upld = "DELETE * " & _
"FROM tbl_CapEx_Projects_Group_Attachements " & _
"WHERE PG_ID=" & var_PGID
Debug.Print Delete_PG_Data
Debug.Print Delete_PG_Upld
db.Execute Delete_PG_Data, dbFailOnError
db.Execute Delete_PG_Upld, dbFailOnError
''Me.Recordset.Delete
''DBEngine.BeginTrans
''DBEngine.CommitTrans
Else
If Me.Saved Then
DBEngine.Rollback
Else
If Me.Dirtied Then DBEngine.Rollback
End If
End If
DoCmd.Close ObjectType:=acForm, ObjectName:=Me.Name
Form_frm_CapEx_Edit_Project_Groups_Cont.Requery
DoCmd.SetWarnings True
End Sub
.. delete a newly created record if the user cancels
Sounds like the record doesn't get saved. Even if not, it could be a timing issue as the run the query in another context than the form.
If it really has been created, the simplest and fastest method is to delete the record from the RecordsetClone of the form.
Use DoCmd.RunSQL
example :
Public Sub DoSQL()
Dim SQL As String
SQL = "UPDATE Employees " & _
"SET Employees.Title = 'Regional Sales Manager' " & _
"WHERE Employees.Title = 'Sales Manager'"
DoCmd.RunSQL SQL
End Sub
Hence your new code will be as below:
Dim var_PGID As String
Dim Delete_PG_Data, Delete_PG_Upld As String
var_PGID = TempVars![var_PG_ID_NEW]
Delete_PG_Data = "DELETE * " & _
"FROM tbl_CapEx_Projects_Group " & _
"WHERE PG_ID=" & var_PGID
Delete_PG_Upld = "DELETE * " & _
"FROM tbl_CapEx_Projects_Group_Attachements " & _
"WHERE PG_ID=" & var_PGID
MsgBox Delete_PG_Data
MsgBox Delete_PG_Upld
DoCmd.RunSQL Delete_PG_Data
DoCmd.RunSQL Delete_PG_Upld
Here is what I have, I'm trying to take fields from an Access form (data comes from one linked sql table) and insert them into another linked sql table:
StrSQL = "INSERT INTO [dbo_Expense_Projection] ([StatusID],[TravellerUFID],[Email]) " & _
VALUES(" & Me.StatusID & ", " Me.TravellerUFID & ", " Me.SubmitterUFID & ", " Me.email & ")
DoCmd.RunSQL StrSQL
But I am getting this error
Compile error: Sub or Function not defined
I think you are just missing some double quotes:
StrSQL = "INSERT INTO [dbo_Expense_Projection] ([StatusID],[TravellerUFID],[Email]) " & _
"VALUES(" & Me.StatusID & ", " Me.TravellerUFID & ", " Me.SubmitterUFID & ", """ & Me.email & """)"
DoCmd.RunSQL StrSQL
You can try to print the contents of StrSQL and check the query before running it:
Debug.Print StrSQL
but I prefer not to create SQL strings with concatenated values (what happens if Me.StravellerUFID contains a double quote?)
I would suggest you to insert data using DAO:
Dim rs as Recordset
Set rs = CurrentDb.OpenRecordset("dbo_Expense_Projection")
rs.AddNew
rs!StatusID = Me.StatusID
rs!TravellerUFID = Me.TravellerUFID
' ...other fields
rs.Update
rs.Close
There are also some ampersands missing in the SQL-String:
StrSQL = "INSERT INTO [dbo_Expense_Projection] ([StatusID],[TravellerUFID],[Email]) " & _
"VALUES(" & Me!StatusID & ", " & Me!TravellerUFID & ", " & Me!SubmitterUFID & ", """ & Me!email & """)"
And I think you should use exclamation mark between "Me" and fieldname. But I do not want to argue with the experts here about that... ;)
Here is what ended up working:
Dim StrSQL As String
StrSQL = "INSERT INTO dbo_Expense_Projection (StatusID,TravellerUFID,Email)
VALUES('" & Form!StatusID & "','" & Form!TravellerUFID & "','" & Form!Email & "')"
DoCmd.SetWarnings False
DoCmd.RunSQL StrSQL
DoCmd.SetWarnings True