Hello
I'm using Access 2003.
With the help of many on this site, I created a button on a form that will allow the user to open up to the folder they wish to get a text file from. The data is saved to a temporary table and if the data is new, it is appended, and if the record exists then the new record overwrites the old. The contents of the temporary table are deleted once the updates/additions are run.
The problem is that the import specs do not appear to be working as created. They were written to indicate that the first row does not contain headers and when I import manually using these specs they are correct and all records are included. But if I import via this import data button, the initial record is not imported...why would that be?
Below is the code for the button:
Any and all assistance greatly appreciated.
I'm using Access 2003.
With the help of many on this site, I created a button on a form that will allow the user to open up to the folder they wish to get a text file from. The data is saved to a temporary table and if the data is new, it is appended, and if the record exists then the new record overwrites the old. The contents of the temporary table are deleted once the updates/additions are run.
The problem is that the import specs do not appear to be working as created. They were written to indicate that the first row does not contain headers and when I import manually using these specs they are correct and all records are included. But if I import via this import data button, the initial record is not imported...why would that be?
Below is the code for the button:
Code:
Private Sub cmdImportData_Click()
On Error GoTo Err_cmdImportData_Click
'Declare a variable as a FileDialog object.
Dim fd As FileDialog
Dim strTableName As String
Dim strSQL_T As String
Dim strSQL_Q1 As String
Dim strSQL_Q2 As String
If MsgBox("Navigate to your data file. Please press 'Cancel' if you do not wish to proceed", _
vbInformation + vbOKCancel, "Raw Data Import") = vbCancel Then GoTo Exit_cmdImportData_Click
DoCmd.SetWarnings False
strTableName = "tmpData"
strSQL_T = "DELETE tmpData.* FROM tmpData"
strSQL_Q1 = "INSERT INTO FLO_Data ( Inst, ChartNo, AcctNo, Age, Sex, Rescode, Postal, AdmDate, AdmTime, AdmDateTime, Ifrom, Entry, Admit, DisDate, DisTime, DisDateTime, Ito, AcuteDays, ALCDays, TotalDays, MOHQ, CMG, CMGDesc, Fyear, Exit, Disp, InstName )" & _
"SELECT tmpData.Inst, tmpData.ChartNo, tmpData.AcctNo, tmpData.Age, tmpData.Sex, tmpData.Rescode, tmpData.Postal, tmpData.AdmDate, tmpData.AdmTime, tmpData.AdmDateTime, tmpData.Ifrom, tmpData.Entry, tmpData.Admit, tmpData.DisDate, tmpData.DisTime, tmpData.DisDateTime, tmpData.Ito, tmpData.AcuteDays, tmpData.ALCDays, tmpData.TotalDays, tmpData.MOHQ, tmpData.CMG, tmpData.CMGDesc, tmpData.Fyear, tmpData.Exit, tmpData.Disp, tmpData.InstName " & _
"FROM tmpData"
strSQL_Q2 = "UPDATE tmpData INNER JOIN FLO_Data ON (tmpData.AcctNo = FLO_Data.AcctNo) AND (tmpData.ChartNo = FLO_Data.ChartNo) AND (tmpData.Inst = FLO_Data.Inst) SET FLO_Data.Inst = [tmpData]![Inst], FLO_Data.ChartNo = [tmpData]![ChartNo], FLO_Data.AcctNo = [tmpData]![AcctNo], FLO_Data.Age = [tmpData]![Age], FLO_Data.Sex = [tmpData]![Sex], FLO_Data.Rescode = [tmpData]![Rescode], FLO_Data.Postal = [tmpData]![Postal], " & _
"FLO_Data.AdmDate = [tmpData]![AdmDate], FLO_Data.AdmTime = [tmpData]![AdmTime], FLO_Data.AdmDateTime = [tmpData]![AdmDateTime], FLO_Data.Ifrom = [tmpData]![Ifrom], FLO_Data.Entry = [tmpData]![Entry], FLO_Data.Admit = [tmpData]![Admit], FLO_Data.DisDate = [tmpData]![DisDate], FLO_Data.DisTime = [tmpData]![DisTime], FLO_Data.DisDateTime = [tmpData]![DisDateTime], FLO_Data.Ito = [tmpData]![Ito], FLO_Data.AcuteDays = [tmpData]![AcuteDays], " & _
"FLO_Data.ALCDays = [tmpData]![ALCDays], FLO_Data.TotalDays = [tmpData]![TotalDays], FLO_Data.MOHQ = [tmpData]![MOHQ], FLO_Data.CMG = [tmpData]![CMG], FLO_Data.CMGDesc = [tmpData]![CMGDesc], FLO_Data.Fyear = [tmpData]![Fyear], FLO_Data.Exit = [tmpData]![Exit], FLO_Data.Disp = [tmpData]![Disp], FLO_Data.InstName = [tmpData]![InstName]"
Application.SetOption "Confirm Action Queries", False
Application.SetOption "Confirm Record Changes", False
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Dim vrtSelectedItem As Variant
Dim strFileName
'Use a With...End With block to reference the FileDialog object.
With fd
'Do Not Allow the selection of multiple file.
.AllowMultiSelect = False
'Use the Show method to display the File Picker dialog box and return the user's action.
'The user pressed the action button.
If .Show = -1 Then
'Step through each string in the FileDialogSelectedItems collection
For Each vrtSelectedItem In .SelectedItems
'vrtSelectedItem is a String that contains the path of each selected item.
'You can use any file I/O functions that you want to work with this path.
DoCmd.Hourglass True
DoCmd.TransferText acImportDelim, "3M_Import_Specification", strTableName, vrtSelectedItem, True
DoCmd.Hourglass False
Next vrtSelectedItem
'The user pressed Cancel.
Else
GoTo Exit_cmdImportData_Click
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
'If DCount("*", "tmpData") > 0 Then DoCmd.RunSQL strSQL_T
If DCount("*", "qryUnmatched") > 0 Then DoCmd.RunSQL strSQL_Q1 Else
DoCmd.RunSQL strSQL_Q2
DoCmd.RunSQL strSQL_T
'DoCmd.SetWarnings True
Application.SetOption "Confirm Action Queries", True
Application.SetOption "Confirm Record Changes", True
Exit_cmdImportData_Click:
DoCmd.Hourglass False
Exit Sub
Err_cmdImportData_Click:
DoCmd.Hourglass False
MsgBox "Import of raw data was not successful", _
vbCritical + vbOKOnly, "Raw Data Import Failed"
MsgBox Err.Description
Resume Exit_cmdImportData_Click
End Sub
Any and all assistance greatly appreciated.