Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / March 2007

Tip: Looking for answers? Try searching our database.

Add a record to an SQL Table

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
richardb - 13 Mar 2007 12:05 GMT
Have learned much programming here but don't yet know how to add a record to
an SQL table from Access. Here is my attempt, but am really guessing after a
certain point and would appreciate help:

Public Function InsertEpisode(PatientID As String, RefPlanNo As Integer)

   On Error GoTo ErrorHandler
   Dim mydb As DAO.Database
   Dim myq As DAO.QueryDef
   Dim myrs As DAO.Recordset
   Dim sqltext As String, strConnect As String, strField As String
   Set mydb = CurrentDb
   Set myq = mydb.CreateQueryDef("")
   
   sqltext = "INSERT INTO Episode (PatUniqueID, FinanceClass, " & _
       "primary_complaint, RefPlanNumber, epsd_date, ts_user) " & _
       "SELECT '" & PatientID & "', FinanceClass, " & _
       "'Created by Scan', CONVERT(varchar, " & RefPlanNo & "), GetDate(),
" & _
       "'Import' FROM Patient WHERE PatUniqueID = '" & PatientID & "' ;"
   strConnect = "ODBC;DSN=PPM_700;;" & _
        "Network=DBMSSOCN;Trusted_Connection=Yes"
   With myq
       .Connect = strConnect
       .SQL = sqltext
       Set myrs = .OpenRecordset
   End With

   With myrs         <------HERE I really don't know!!!
       .AddNew
       .Update
       .Close
   End With

InsertEpisode_Exit:
  Set myrs = Nothing
   myq.Close
   Set myq = Nothing: Set mydb = Nothing
   Exit Function

ErrorHandler:
   MsgBox Err.Number & " - " & Err.Description, vbCritical, "Error in
InsertEpisode"
   Resume InsertEpisode_Exit
   
End Function
Stefan Hoffmann - 13 Mar 2007 12:11 GMT
hi Richard,

> Have learned much programming here but don't yet know how to add a record to
> an SQL table from Access. Here is my attempt, but am really guessing after a
> certain point and would appreciate help:
You are mixing the recordset and SQL approach.

>     strConnect = "ODBC;DSN=PPM_700;;" & _
>          "Network=DBMSSOCN;Trusted_Connection=Yes"
Is this a SQL Server?

The easiest way is to create a linked table and use:

a) CurrentDb.Execute "INSERT INTO linkedTable ..."

or

b) Set rs = CurrentDb.OpenRecordset("linkedTable")
   rs.Add
   rs![Fields] = Values
   rs.Update

mfG
--> stefan <--
richardb - 13 Mar 2007 12:28 GMT
Stefan,

This would be an SQL database, so the table is not linked.

> hi Richard,
>
[quoted text clipped - 20 lines]
> mfG
> --> stefan <--
Stefan Hoffmann - 13 Mar 2007 12:42 GMT
hi Richard,

> This would be an SQL database, so the table is not linked.
Are you using a .mbd or .adp?

mfG
--> stefan <--
richardb - 13 Mar 2007 13:11 GMT
.mdb

> hi Richard,
>
[quoted text clipped - 3 lines]
> mfG
> --> stefan <--
Stefan Hoffmann - 13 Mar 2007 13:52 GMT
hi Richard,

> .mdb
So link the table or use ADO.Connection.

mfG
--> stefan <--
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.