SQL STATEMENT to insert fields in a table from a query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TSIGOS1
    New Member
    • Sep 2007
    • 24

    SQL STATEMENT to insert fields in a table from a query

    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!
  • Kevin Wilcox
    New Member
    • Sep 2007
    • 68

    #2
    Originally posted by TSIGOS1
    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.sfrmTriageHi dden!cboDecisio n.Column(0)
    MyProviderID = Me.sfrmTriageHi dden!cboProvide r.Column(0)
    MyStatusID = 1 'Pending
    MyDiagID = Me.sfrmTriageHi dden!cboDiagID. Column(0)
    MyOriginEventID = Me.sfrmTriageHi dden!EventID
    MyCaseID = Me.sfrmTriageHi dden!CaseID

    DoCmd.SetWarnin gs False
    MySQL = "INSERT INTO tblEvents (DatePosted, ServiceID, ProviderID, StatusID, DiagID," & _
    "OriginEven tID, 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.SetWarnin gs 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

    Comment

    • TSIGOS1
      New Member
      • Sep 2007
      • 24

      #3
      hello,
      Thnks for the answer!
      with your help I came up with the following sql statement which works :
      INSERT INTO [TBL] select * from [QUERY] ;


      thanks a lot again

      Comment

      Working...