The text file specification does not exist - ms-access

I'm trying to run a simple access program that exports data from a select query. However I keep getting the error:
Run-time error '3625'
The text file specification 'Deposits Link Specification' does not exist. You cannot import, export, or link using the specification.
I didn't set up a text file specification, mainly because I haven't had to do this before. How would I go about fixing this?
My simple code, is below:
'*************************************************************************
Public Function startupdate()
'*************************************************************************
DoCmd.SetWarnings False
DoCmd.TransferText acExportDelim, , "DepositsToChecklist", "X:\InHouseApps\SummerCamps\TMS\EEChecklistImport.csv", True
DoCmd.OpenQuery "qryAppendDepositsToChecklist"
DoCmd.Quit
End Function

Related

MS Access Library Template - Reference Category Value in VBA

Completely new to MS Access - I'm trying to do a simple modification to the MS Access Lending Library template using VBA.
Going into the Asset List form and clicking New Asset pops up the Asset Details form, where I can click the Save and New button. This calls the VBA code cmdSaveandNew_Click().
My question is, within this VBA method, how do I reference the values of the fields in the Asset Details form - For example getting the value that the user entered in the Category field and storing it in a variable.
I've tried several strings of code to try to get the value back, but I keep getting various errors, saying it can't find the referenced form, or Object Required, or this method is supported. I think it's likely just that I don't fully understand the structure of what I'm trying to reference or the syntax for specifying how to reference it.
Right now (after I've been trying many different combinations of things, I have:
text = Forms![Asset List].[Asset Details].Form.Category
which is giving me the "can't find the field '|1' referred to in your expression" error.
Edit - code currently looks like this:
Private Sub cmdSaveandNew_Click()
'On Error GoTo cmdSaveandNew_Click_Err
Dim someVar As String
Dim num As Integer
Dim objtf
'objtf = Forms![Asset List].[Asset Details].Form.Category
objtf = Me.Category
'num = Form.Controls.Count
Msg.Box (TypeName(objtf))
someVar = InputBox("How many?")
On Error Resume Next
If (Form.Dirty) Then
DoCmd.RunCommand acCmdSaveRecord
End If
If (MacroError.Number <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
Exit Sub
End If
On Error GoTo 0
DoCmd.GoToRecord , "", acNewRec
DoCmd.GoToControl "Item"
MsgBox someVar
cmdSaveandNew_Click_Exit:
Exit Sub
cmdSaveandNew_Click_Err:
MsgBox Error$
Resume cmdSaveandNew_Click_Exit
End Sub
The variable must be declared global in a general module if you want to reference from various modules. Then setting it by code behind the Asset Details form would be simply: SomeVariableName = Me.Category.
Don't use reserved words as names - text is a reserved word.

DoCmd.DeleteObject acTable - The record ... specified on this form or report does not exist

Let me start by saying I am not at all familiar with Access or VBA. However, I am the IT guy and at some point someone created an MS Access database that does a thing and now I have to support it.
We have a database that upon opening deletes any old data and re-runs the external query that makes this application work. Occasionally whatever state the program exited out in that table already does not exist. This causes MS Access to hang on the delete line and I have to run the debugger, comment out the DoCmd.DeleteObject line, re run, and then un-comment the line to let the user continue with their day.
I want to add in some sort of conditional statement, but anything I've been able to google in terms of If...Then statements or 'TableExist' type functions always causes an error to be thrown that I haven't defined the function. My best guess is I'm nesting this incorrectly or I'm not calling some sort of external function correctly, but as I said my VBA knowledge is extremely limited.
This code executes on startup:
Public Function Startup() As Integer
DoCmd.Close acForm, "soLookup"
DoCmd.DeleteObject acTable, "sales_order"
DoCmd.RunSavedImportExport "Import-sales_order"
DoCmd.OpenForm "soLookup"
End Function
Its the
DoCmd.DeleteObject acTable, "sales_order"
Line that causes things to fail.
I've attempted to restructure this several times based on several examples I had found, but I'll only bother with one below
Public Function Startup() As Integer
DoCmd.Close acForm, "soLookup"
If TableExists("sales_orders") Then
DoCmd.DeleteObject acTable, "sales_orders"
Else
'Do nothing
End If
DoCmd.RunSavedImportExport "Import-sales_order"
DoCmd.OpenForm "soLookup"
End Function
Nothing I seem to try seems to give me any result other than an error of some sort. All I want to do is add a conditional statement to this 'Startup' bit that checks if the "sales_order" table even if exists, and if it doesn't, then to just move on to the next comment and forget the DoCmd.DeleteObject. How can I achieve this functionality?! Thanks!
The TableExists function is not a standard function in Access. You need to define it yourself.
There are two main ways to define such a function, by trying and error trapping, or by iterating through all tables and checking names.
Way 1 (error trapping):
Public Function TableExists(TableName As String) As Boolean
On Error Resume Next
TableExists = CurrentDb.TableDefs(TableName).Name = TableName
End Function
Way 2 (iterating collection):
Public Function TableExists(TableName As String) As Boolean
Dim td As DAO.TableDef
For Each td In CurrentDb.TableDefs
If td.Name = TableName Then
TableExists = True
Exit Function
End If
Next
End Function
Define either of these functions in a public module, and your last approach should work
I will instead modify the query to import the source data into a new table (sales_orders), which will overwrite existing data when the query runs and so I don't have to delete the table and have additional check for TableExists

Access VBA Macro to run pass through query

I have a pass through query built in Teradata set to export data to an Excel spreadsheet. I'm trying to automate it, but when I run the macro or open the query, a window pops up asking for the data source. I have an ODBC connection created and I'm thinking there has to be a way to make the macro pass the data source name so it will run without interaction.
Edit: Adding Macro as requested
Function AutoExec()
On Error GoTo AutoExec_Err
DoCmd.OutputTo acOutputQuery, "Performance Interval Data", "ExcelWorkbook(*.xlsx)", _
"filepath\filename.xlsx", False, "", , acExportQualityPrint
DoCmd.Quit acExit
AutoExec_Exit:
Exit Function
AutoExec_Err:
MsgBox Error$
Resume AutoExec_Exit
End Function
Couple of concerns, (can't validate any of this right now as I do not currently have access to Access for testing), but it looks like:
You're trying to OutputTo a query, to the best of my knowledge that
is not feasible.
Your file path is setup as filepath\filename.xlsx unless that is the actual location and name of your Excel sheet, something seems
wrong there to me.
I don't really think this macro relates to an ODBC of any sort in its current state.
But, you should at least start with fixing the filepath issue. That should be the full path to your Excel file and the full name of the file as well. (i.e. C:\TEMP\TestExcelSheet.xlsx)
All that being said, you may want to just go with something like this (although its a little difficult to tell if this is what you actually want or not):
'Export Excel file from Query
DoCmd.TransferSpreadsheet acExport, , "acOutputQuery", _
"C:\TEMP\TestExcelSheet.xlsx", True
NOTE: "acOutputQuery" should be the actual name of your passthrough query, "C:\TEMP\TestExcelSheet.xlsx" would be your destination path, and True adds the query's headers into the sheet, False to ignore the headers.

TransferText incorrectly imports strings like "FS###" as Currency

I am using the VBA DoCmd.TransferText command to import data from a CSV text file into a new table in my Access database. I have run into an issue where the text data in the first three columns in some of the files is imported as currency. I cannot figure out what is happening.
Here is a test database along with one CSV that imports correctly (VollintineLines.csv) ...
PipeID,UpstreamMH,DownstreamMH,Diameter,GISLength,Status
WS010353S,WS010353,WS010163,36,227.1984614,Fully Surveyed as Phase Work
WS011155S,WS011155,WS011154,8,418.5435318,Not Surveyed
WS011154S,WS011154,WS011153,8,303.9618911,Fully Surveyed as Phase Work
... and one that doesn't (CourtLines.csv).
PipeID,UpstreamMH,DownstreamMH,Diameter,GISLength,Status
FS020628S,FS020628,FS020462,10,278.72,Not Surveyed
FS020463S-1,FS020463,FS020462,12,248.39,Not Surveyed
FS020216S,FS020216,FS020215,12,227.53,Fully Surveyed as Phase Work
(Please ignore the unnamed objects in the database, it was just to figure out what is going on here and I didn't bother naming things.)
Here is the import code, you have to enable the Microsoft Office 16.0 Object Library Reference.
Private Sub Command0_Click()
Dim Path As FileDialog
Dim FileName As Variant
DoCmd.SetWarnings False
DoCmd.Hourglass True
Set Path = Application.FileDialog(msoFileDialogFilePicker)
With Path
.AllowMultiSelect = False
.Title = "Select your File"
.Filters.Add "All Files", "*.*"
If .Show = -1 Then
For Each FileName In .SelectedItems
DoCmd.TransferText acImportDelim, , "TempPipeData", FileName, True
Next FileName
Else
MsgBox "No File Selected to Import."
End If
End With
DoCmd.SetWarnings True
DoCmd.Hourglass False
End Sub
You have apparently encountered a rather obscure bug affecting TransferText calls that do not use an Import Specification. (It is also discussed on another site here.)
Workarounds include:
Use an Import Specification as described in this answer.
Create the table first, specifying the desired column types (Text in this case), and then import from the CSV file into the existing (empty) table.
If neither of the above options is desirable, then you could use COM Automation to
launch an instance of Excel,
have Excel open the CSV file,
save it to XLS or XLSX,
use TransferSpreadsheet in Access VBA to import the Excel data, then
delete the temporary XLS[X] file.

Invalid Argument on import code that works elsewhere

At a loss, I have an import module used to help my less technically proficient coworkers to import data to an access database for processing. I use the following code for 8 difference text imports, they all work except for one. I can manually import using the import specification so that is not the issue, I have triple checked the table and import spec name, also not the issue, can anyone tell me why access is throwing the error?
Function import_Headcount()
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Set db = CurrentDb
Dim path As Variant
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM [tbl_Headcount]"
With fd
.AllowMultiSelect = False
If .Show = -1 Then
For Each path In .SelectedItems
DoCmd.TransferText acImportDelim, "National Headcount", "tbl_Headcount", path, False
Next path
Else
MsgBox ("Import for Headcount cancelled")
Exit Function
End If
End With
Set fd = Nothing
db.Execute "qry_Update_Headcount_Fields"
DoCmd.SetWarnings True
MsgBox ("Import of Headcount complete")
End Function
It is throwing a "3001" error on the DoCmd.TransferText line, as I have said I have copy and pasted this small function 8 times with different tables and import specs and cannot see why this one is not working. Thanks for the help!
Have you tried to use square brackets in specification name?
DoCmd.TransferText acImportDelim, "[National Headcount]", "tbl_Headcount", path, False
Sounds like you are on the right track on the troubleshooting side... Keep ruling out the variables until you can pinpoint the issue.
I would suggest installing V-Tools, a freeware Access add-in. This has a utility for managing/editing import specs. This would be the first tool I would reach for in debugging the import issue. It includes a number of other helpful tools, but for the project at hand, I think you will find the Import/Export Specifications form very helpful.
Here is a screen shot of an import spec we use on a daily basis:
As you can see, this utility exposes far more options than the built-in wizards when it comes to importing and exporting data.
Hope that helps!