Hello all,
can somebody help me out please, I try to create an sql statement to insert into a table everything that there is in a specific query. Including the Field names.
Thanks very very much!
TSIGOS1
Do you mean how do you create an sql in VBA to append to a table? If so here's one example;
Dim MySQL As String
Dim MyDiagID As Long
Dim MyServiceID As Long
Dim MyCaseID As Long
Dim MyOriginEventID As Long
Dim MyStatusID As Long
Dim MyProviderID As Long
MyServiceID = Me.sfrmTriageHidden!cboDecision.Column(0)
MyProviderID = Me.sfrmTriageHidden!cboProvider.Column(0)
MyStatusID = 1 'Pending
MyDiagID = Me.sfrmTriageHidden!cboDiagID.Column(0)
MyOriginEventID = Me.sfrmTriageHidden!EventID
MyCaseID = Me.sfrmTriageHidden!CaseID
DoCmd.SetWarnings False
MySQL = "INSERT INTO tblEvents (DatePosted, ServiceID, ProviderID, StatusID, DiagID," & _
"OriginEventID, CaseID) " & _
"SELECT Date() as DatePosted, """ & MyServiceID & """ as ServiceID" & _
", """ & MyProviderID & """ as ProviderID, """ & MyStatusID & """ as StatusID " & _
", """ & MyDiagID & """ as DiagID, """ & MyOriginEventID & """ as OriginEventID" & _
", """ & MyCaseID & """ as CaseID;"
DoCmd.RunSQL MySQL
DoCmd.SetWarnings True
I prefer to declare pretty much all my variables and put them into the statement like so, mainly because it makes my code easier for me to read and understand/debug, but you could also put the fieldnames in directly.
If you meant you also wanted to insert the fieldnames themselves into your table I'd hazard a guess that what you really want is to make a table using the field names from your query? In which case you can replace my DoCmd.RunSQL MySQL line with code using the createtabledef method, well-documented in the Help files