schniggeldorf
Registered User.
- Local time
- Today, 12:44
- Joined
- Jan 7, 2013
- Messages
- 22
I wrote some code to evaluate each email as it arrives in MS outlook, evaluate whether it contained useful data, and copy the data from the body of the text. I then wrote the following Sub to send that data to an existing Access database. Everything works exactly as intended when I first run it. However, when I run it again, I get Error 462: The remote server machine does not exist or is unavailable.
I've read multiple posts suggesting that I need to fully specify each object, but as far as I can tell, I've done that already. The code throwing the error is marked in red.
Thanks for your help.
I've read multiple posts suggesting that I need to fully specify each object, but as far as I can tell, I've done that already. The code throwing the error is marked in red.
Code:
Sub ExportToAccess(MRN As Long, PatientLastName As String, PatientFirstName As String, AttendingLastName As String, _
AttendingFirstName As String, strEncounterDate As String, Optional Status As Integer, Optional PendingType As Integer, Optional ClosedType As Integer)
' Transfer data from emails (form responses) to Access
Dim appAccess As Access.Application
Dim wrkspc As DAO.Workspace
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strMonth As String
Dim Month As Integer
Dim Day As Integer
Dim Year As Integer
Dim EncounterDate As Date
Dim CcfMdId As Integer
Dim SQL1 As String
Dim SQL2 As String
On Error GoTo Err_ExportToAccess
Set appAccess = CreateObject("Access.Application")
[COLOR="Red"]Set wrkspc = DBEngine.Workspaces(0) 'This is the bug!![/COLOR]
Set db = wrkspc.OpenDatabase(constDbPath)
Set rst = db.OpenRecordset("tblKpEmailUpload", dbOpenDynaset) 'Specify the table where the data go after they're processed (e.g. tblPatients).
rst.FindFirst "MRN= " & MRN
'If record already exists, edit. Else add new.
If rst.NoMatch = False Then 'If recExist > 0 Then
rst.Edit
Else
rst.AddNew
rst.Fields("MRN").Value = MRN
End If
rst.Fields("PatientFirstName").Value = StrConv(PatientFirstName, vbProperCase)
rst.Fields("PatientLastName").Value = StrConv(PatientLastName, vbProperCase)
rst.Fields("AttendingFirstName").Value = AttendingFirstName
rst.Fields("AttendingLastName").Value = AttendingLastName
EncounterDate = CDate(Mid(strEncounterDate, 4, 2) & "-" & Left(strEncounterDate, 3) & "-" & Right(strEncounterDate, 4))
rst.Fields("EncounterDate").Value = EncounterDate
rst.Fields("Status").Value = Status
rst.Fields("PendingType").Value = PendingType
rst.Fields("ClosedType").Value = ClosedType
rst.Update
' Run queries to append this record to tblPatients in IMATCH Data File.accdb, and to delete the records in tblKpEmailUpload afterwards
SQL1 = "INSERT INTO tblPatients (MRN, FirstName, LastName, RefDate, Status, PendingType, ClosedType, PatientCcfHaMdId )" _
& "SELECT tblKpEmailUpload.MRN, tblKpEmailUpload.PatientFirstName, tblKpEmailUpload.PatientLastName, tblKpEmailUpload.EncounterDate, " _
& "tblKpEmailUpload.Status, tblKpEmailUpload.PendingType, tblKpEmailUpload.ClosedType, tblCcfHeadacheStaff.CcfStaffID " _
& "FROM tblKpEmailUpload INNER JOIN tblCcfHeadacheStaff ON (tblKpEmailUpload.AttendingLastName = tblCcfHeadacheStaff.LastName) " _
& "AND (tblKpEmailUpload.AttendingFirstName = tblCcfHeadacheStaff.FirstName);"
SQL2 = "Delete tblKpEmailUpload.MRN " _
& "From tblKpEmailUpload " _
& "WHERE (((tblKpEmailUpload.MRN)>0));"
db.Execute SQL1 'This uploads the new data from tblKpEmailUpload to tblPatients
db.Execute SQL2 'This empties tblKpEmailUpload
' Close open objects
rst.Close
Set rst = Nothing
db.Close
Set db = Nothing
wrkspc.Close
Set wrkspc = Nothing
appAccess.Quit
Set appAccess = Nothing
Exit_ExportToAccess:
Exit Sub
Err_ExportToAccess:
MsgBox Err.Number & ": " & Err.Description
Resume Exit_ExportToAccess
End Sub
Thanks for your help.