Make Table statement issue

creyn

Board Regular
Joined
Sep 16, 2016
Messages
127
Can someone help with why this Create Table statement is not working correctly? Below is the code I am using. I keep getting a Run-time error '91': Object variable or With block variable not set.

Public Sub Command0_Click()
Dim MyFolder As String
Dim MyFile As String
Dim MyFolder2 As String
Dim DQ As String
Dim MyData As String
Dim myQueryName As String
Dim myExportFileName As String
Dim sysdate As Date
Dim dbs As Database
Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
Dim fld As Folder
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.Title = ""
.AllowMultiSelect = False
Intialfilename = ""

If .Show = True Then
varfile = .SelectedItems(1)
strFolder = varfile
Else
rtn = SysCmd(4, "Action aborted by user")
Exit Sub
End If

DoCmd.SetWarnings False
dbs.Execute "Create Table Import_Data([Field1] Text(100) NOT NULL, [FileName] Text(100) NOT NULL, Date Date/Time);"
MyFolder = varfile
MyFile = Dir(MyFolder)
Call fso.CopyFile(MyFolder, MyFile & ".txt")
MyFile = Dir(MyFolder & ".txt")
DoCmd.SetWarnings False
DoCmd.RunSQL "Delete * from Import_Data"
DoCmd.TransferText acLinkDelim, "Import_Data Specification", "Import_Data", MyFile, True
DoCmd.RunSQL "UPDATE Import_Data SET FileName = '" & MyFile & "', [Date] = '" & Date & "' WHERE FileName IS NULL And Date is NULL"
DoCmd.RunSQL "INSERT INTO SpecialCharacter_Errors ( Field1, FileName, [Date] ) SELECT Special_Characters.Field1, Special_Characters.FileName, Special_Characters.Date " & _
"FROM Special_Characters;"
DoCmd.OpenQuery "Qry_Import_Data"
DoCmd.Close acQuery, "Qry_Import_Data"
DoCmd.OpenQuery "Column_Export"
DoCmd.Close acQuery, "Column_Export"
DoCmd.TransferText acExportDelim, "Export_Data Specification", "Column_Export", [MyFile], False
DoCmd.SetWarnings False
MsgBox "Process Completed"
End With
Set fso = Nothing
dbs.Execute "Drop TABLE Import_Data;"
dbs.Close
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
if would use queries, instead of sql, the query will tell you what is wrong.
there's no need to write all this code.

Can someone help with why this Create Table statement is not working correctly? Below is the code I am using. I keep getting a Run-time error '91': Object variable or With block variable not set.

Public Sub Command0_Click()
Dim MyFolder As String
Dim MyFile As String
Dim MyFolder2 As String
Dim DQ As String
Dim MyData As String
Dim myQueryName As String
Dim myExportFileName As String
Dim sysdate As Date
Dim dbs As Database
Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
Dim fld As Folder
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.Title = ""
.AllowMultiSelect = False
Intialfilename = ""

If .Show = True Then
varfile = .SelectedItems(1)
strFolder = varfile
Else
rtn = SysCmd(4, "Action aborted by user")
Exit Sub
End If

DoCmd.SetWarnings False
dbs.Execute "Create Table Import_Data([Field1] Text(100) NOT NULL, [FileName] Text(100) NOT NULL, Date Date/Time);"
MyFolder = varfile
MyFile = Dir(MyFolder)
Call fso.CopyFile(MyFolder, MyFile & ".txt")
MyFile = Dir(MyFolder & ".txt")
DoCmd.SetWarnings False
DoCmd.RunSQL "Delete * from Import_Data"
DoCmd.TransferText acLinkDelim, "Import_Data Specification", "Import_Data", MyFile, True
DoCmd.RunSQL "UPDATE Import_Data SET FileName = '" & MyFile & "', [Date] = '" & Date & "' WHERE FileName IS NULL And Date is NULL"
DoCmd.RunSQL "INSERT INTO SpecialCharacter_Errors ( Field1, FileName, [Date] ) SELECT Special_Characters.Field1, Special_Characters.FileName, Special_Characters.Date " & _
"FROM Special_Characters;"
DoCmd.OpenQuery "Qry_Import_Data"
DoCmd.Close acQuery, "Qry_Import_Data"
DoCmd.OpenQuery "Column_Export"
DoCmd.Close acQuery, "Column_Export"
DoCmd.TransferText acExportDelim, "Export_Data Specification", "Column_Export", [MyFile], False
DoCmd.SetWarnings False
MsgBox "Process Completed"
End With
Set fso = Nothing
dbs.Execute "Drop TABLE Import_Data;"
dbs.Close
End Sub
 
Upvote 0
You aren't setting dbs to refer to, well, anything.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,681
Members
449,116
Latest member
HypnoFant

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top