Error - 'Cannot execute data definition'?

Jim Dudley

Registered User.
Local time
Today, 12:53
Joined
Feb 16, 2012
Messages
81
I have an application that imports data from Excel via a module 'bas_import_functions'.

I am not a programmer.

Issue:
When calling the 'Upload Attendance' function, I receive and error message 'Cannot execute data definition statements on linked data sources'.

What does this mean?

When I check the table imported to by the function, all the new data is present.

I have checked the 'Linked Table Manager' and all the Links seem to be fine.

The path to the application back-end is;
C:\LDP_Database\LDP_Back_End\LDP_V2.1_be

The path to the application front-end is:
C:\LDP_Database\LDP_V2.2a_fe

Any suggestions as to what is causing this error would be appreciated.

Jim
 
Last edited:
1. The thing apparently messes with the tables - it tries to redesign a table which is a nono for a linked table

2. When you post questions - show the failing bit of code and indicate which line. Our remote viewing/mind reading abilities are fairly limited :D
 
Is there a way of posting just the code or do I have to post the whole application?

If just the code can be posted, how is that done? I am new to this.

The error that I descriped is quoted as it appears on the screen. It does not stop the code or give any inidcation of where in the code the error occurs.

Jim
 
It is presumably your bas_import_functions that plays havoc. And perhaps it has some error handling, that muffles the debugger. Go into that module and comment out all ON ERROR statements, run again and post the failing code.

If you don't know how to do that, then

1. Post the db
2. Post the sample file you are importing
3. Post specific instructions as to how to recreate the error
4. Hope that someone will look into it :D
 
Here is the specific error Line:

CurrentDb.Execute "INSERT INTO " & strImportHoldingTable & _
" SELECT " & strImportTable & ".* " & _
"FROM " & strImportTable

Error:
Run time error '3611':
Cannot execute data definintion statements on linked data source.
Line 203,Col1

127 Select Case strImportTable
Case "tmp_Attendance"
CurrentDb.Execute ("ALTER TABLE " & strImportHoldingTable & " ALTER COLUMN SNUM Text") 'Ensure SNUM is in text format
End Select 'Append the records. Iterate through the recordset to get each field name to run the append query



All Code:

Option Compare Database
Option Explicit

Public Function LocateFiles()
'*******************************************************************************************************'
'Function to allow for multiple file selections. Function determines which form is currently being used '
'and performs appropriate actions such as populating list-box for the current form that is open and '
'being used. *****Data Gopher 8/3/2011 '
'*******************************************************************************************************'
'On Error GoTo Err_Error_Handling
'Variables to identify current open form
Dim frmCur As Form
Dim objFrm As Object
Dim varFrm As Variant
Dim strcurfrm As String
'Locate file variables
Dim fDialog As Office.FileDialog
Dim varFile As Variant
Dim varFileName As Variant
Dim varFilePath As String
'Variables to pass values
Dim intRetVal As Integer


'Get the current form that is being used
100 Set objFrm = Application.Forms 'All forms collection
101 For Each frmCur In objFrm 'Loop all forms to fine the one that is open
102 If Forms(frmCur.Name).CurrentView <> 0 Then
103 strcurfrm = frmCur.Name
104 End If
105 Next frmCur

'Perform the file selection portion
'Clear listbox contents.
'Forms!frm_Upload_Attendance.lstFiles.RowSource = ""
110 Forms(strcurfrm).lstFiles.RowSource = ""

111 Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
112 With fDialog
'Allow user to make multiple selections in dialog box
113 .AllowMultiSelect = True

'Set the title of the dialog box.
114 .Title = "Please select one or more files to import"

'Clear out the current filters, and add our own.
115 .Filters.Clear
116 .Filters.Add "Excel Spreadsheets", "*.XLS,*.XLSX"

'Ensure the user selects atleast one file
120 If .Show = True Then
'iterate through all selections and add to the list box
121 For Each varFile In .SelectedItems
122 varFileName = Split(varFile, "\")
123 varFileName = varFileName(UBound(varFileName))
124 varFilePath = varFile & "," & varFileName
125 Forms(strcurfrm).lstFiles.AddItem varFilePath
126 Next
127 Else
128 MsgBox "You clicked Cancel in the file dialog box."
129 End If
130 End With
'Error trapping
Exit_Error_Handling:
Exit Function

Err_Error_Handling:
MsgBox Err.Description
Resume Exit_Error_Handling
' intRetVal = GetSelections(strcurfrm)
End Function

Public Function GetSelections()
'***************************************************************************************************************************'
'Gets the current form that is open and iterates through the list box to get each file that was selected and then passes '
'the values to a separate function for importing. *****Data Gopher 8/3/2011 '
'***************************************************************************************************************************'
'On Error GoTo Err_Error_Handling

'Form variables
Dim frmCur As Form
Dim objFrm As Object
Dim strcurfrm As String
'List Box variables
Dim x As Integer
Dim ctlFiles As Control
Dim strFileName As String
'Variables to pass values to other functions
Dim intRetVal As Integer
Dim strImportTable As String
Dim strImportHoldingTable As String
Dim intImportCount As Integer
Dim strDBTable As String
Dim strFileAdd As String
Dim dtDate As Date
Dim intTotalCount As Integer
'Set to the current date
dtDate = Date
'Dim intImportCount As Integer


'Reset the count
intImportCount = 1
'Get the current form that is being used
100 Set objFrm = Application.Forms 'All forms collection
101 For Each frmCur In objFrm 'Loop through all forms to find the one that is open
102 If Forms(frmCur.Name).CurrentView <> 0 Then
103 strcurfrm = frmCur.Name
104 End If
105 Next frmCur
106 strImportTable = "tmp_" & Mid(strcurfrm, 12, Len(strcurfrm) - 11) 'Get the name of the import table
107 strImportHoldingTable = strImportTable & "_Holding"
108 strDBTable = "tbl_" & Mid(strcurfrm, 12, Len(strcurfrm) - 11) 'Get the name of the import table
'Set the control
110 Set ctlFiles = Forms(strcurfrm).lstFiles

111 For x = 0 To ctlFiles.ListCount - 1
112 strFileName = ctlFiles.ItemData(x)
113 intRetVal = ImportOps(strImportTable, strImportHoldingTable, strFileName, intImportCount, strDBTable)
'Append to the import history. Added 1/2/2012
strFileAdd = Mid(strcurfrm, 12, Len(strcurfrm) - 11)
intTotalCount = DCount("*", strImportTable)
CurrentDb.Execute "INSERT INTO tbl_Import_History ( ih_File_Name, ih_Import_Date, ih_Total_Records ) " & _
"VALUES (""" & strFileAdd & """, #" & Date & "#, " & intTotalCount & ")"

114 intImportCount = intImportCount + 1

115 Next x

DoCmd.OpenTable "tbl_Import_History"

intRetVal = CheckForNew(strImportTable, strDBTable, strImportHoldingTable)


'Error trapping
Exit_Error_Handling:
Exit Function

Err_Error_Handling:
MsgBox Err.Description
Resume Exit_Error_Handling
End Function

Public Function ImportOps(strImportTable As String, _
strImportHoldingTable As String, _
strFileName As String, _
intImportCount As Integer, _
strDBTable As String)
'***************************************************************************************************************'
'The name of the file to import and table to import is passed from the Function GetSelections. First a check '
'is performed to determine if the import table already exists and if it does, it's deleted. Each file is then '
'imported to its respective table. *****Dan Hafdelin 8/4/2011 '
'***************************************************************************************************************'
'On Error GoTo Err_Error_Handling
Dim objTbl As Object
Dim tdf As TableDef
Dim strField As String
Dim strFieldSQL As String
Dim strFieldInsertSQL As String
Dim strFieldHoldingSQL As String
Dim fld As Field
Dim dbs As Database
Set dbs = CurrentDb
Dim intRetVal As Integer


'Check to deterimine if the import table exists and if so delete it
100 For Each objTbl In CurrentDb.TableDefs
101 If objTbl.Name = strImportTable Then
102 DoCmd.DeleteObject acTable, strImportTable
103 End If
104 Next objTbl

'Import the spreadsheet
110 DoCmd.TransferSpreadsheet acImport, , strImportTable, strFileName, -1
'Check if the temporary holding table exists and if so and this is the first file being imported, it is deleted
'120 If intImportCount = 1 Then
'121 For Each objTbl In CurrentDb.TableDefs
'122 If objTbl.Name = strImportHoldingTable Then
'123 DoCmd.DeleteObject acTable, strImportHoldingTable
'124 End If
'125 Next objTbl
'126 CurrentDb.Execute "SELECT " & strImportTable & ".* INTO " & strImportHoldingTable & _
' " FROM " & strImportTable
'If a first pass then delete records from holding table
If intImportCount = 1 Then
CurrentDb.Execute "DELETE " & strImportHoldingTable & ".* " & _
"FROM " & strImportHoldingTable
End If


CurrentDb.Execute "INSERT INTO " & strImportHoldingTable & _
" SELECT " & strImportTable & ".* " & _
"FROM " & strImportTable



127 Select Case strImportTable
Case "tmp_Attendance"
CurrentDb.Execute ("ALTER TABLE " & strImportHoldingTable & " ALTER COLUMN SNUM Text") 'Ensure SNUM is in text format
End Select 'Append the records. Iterate through the recordset to get each field name to run the append query


'Iterate through the table to get each field name

131 Set tdf = dbs.TableDefs(strImportHoldingTable)
132 For Each fld In tdf.Fields
133 strField = "[" & fld.Name & "], "
134 strFieldSQL = strFieldSQL & strField
135 strFieldInsertSQL = strFieldInsertSQL & strImportTable & "." & strField

136 Next fld
137 strFieldSQL = Left(strFieldSQL, Len(strFieldSQL) - 2)
138 strFieldInsertSQL = Left(strFieldInsertSQL, Len(strFieldInsertSQL) - 2)

'End If

'Add New Records
'intRetVal = CheckForNew(strImportTable, strFieldSQL, strDBTable, strFieldHoldingSQL, strImportHoldingTable)
'Import the file(s) to the temporary table and then append to holding records table

'Error trapping
Exit_Error_Handling:
Exit Function

Err_Error_Handling:
MsgBox Err.Description
Resume Exit_Error_Handling
End Function

Public Function CheckForNew(strImportTable As String, _
strDBTable As String, _
strImportHoldingTable As String)

Dim strNewRecs As String
Dim strMatching As String
Dim strCleanRecs As String
Dim strRemaining As String
Dim strUpdate As String
Dim strDelBlank As String

'Delete existing records

'Clean Temp records


strDelBlank = "qdel_Blank_" & strImportTable
strNewRecs = "qapp_NR_" & strImportTable

'Delete extra spaces
CurrentDb.Execute strDelBlank, dbFailOnError

'Add new records and update information
CurrentDb.Execute strNewRecs, dbFailOnError



'Append remaining records after update
strRemaining = "qapp_Rem_" & strImportTable
CurrentDb.Execute strRemaining, dbFailOnError

'Update the records
strUpdate = "qupd_" & strImportTable
'CurrentDb.Execute strRemaining, dbFailOnError




End Function


Jim
 
This bit

CurrentDb.Execute ("ALTER TABLE " & strImportHoldingTable & " ALTER COLUMN SNUM Text")

tries to change the table, and the table is apparently linked.

It is hard to tell what to do abut all this, because why does the thing attempt to mess with a linked table?

If this is a one-off excercise, then you could unlink the tables from any backend and do all the stuff locally. For a repeated performance, that would require looking into the entire application.
 
I will live with it for now. It does not seem to have any impact on the outcome. I am going to try with the help of the forum to re-write the code for this import function. As I indicated, I am striving to obtain novice level. I think the Alter table a function to modify a field in the temp table (make sure the content is text not number format and then go on to append the records to the final table. We originally had a problem with students inputing the Student ID Number as a number and not text in Excel Spreadsheets. This bit of code was to automatically correct the situation and was adopted prior to the database being split.

Thanks for you input. I feel I at least know what the error is about.

Regards,

Jim
 
Something just dawned on me. The Original Code manipulated some field elements in the import process. e.g. [SNum] needs to be a text field. It is a unique 9 digit Identifier for each student. Excel likes to treat it as a number. The code verifies that it is text or converts it to text if it is numeric. This is done in tmp_tables prior to appending acceptable records from the import to the main/final table. There are three types of tables that are imported from Excel. Access in not widely available in the Colleges/Universities but Excel is Universally available.

Deduction:
The code needs to adjusted for the split database and the imports need to be done to the back-end file.

Would that be correct and if so, can the code tell that it should be dealing with tables not linked data?

Any input would be appreciated.

Jim
 
Beleive it or not, you can modify the structure of the table that is represented by the linked table object fairly easily ... just provide the "Fully Qualified" path to the source table via the .Connect property of the linked table object.

Code:
[FONT=Calibri]CurrentDb.Execute "ALTER TABLE [" & CurrentDb.TableDefs(strImportHoldingTable).Connect & "].[" & strImportHoldingTable & "] ALTER COLUMN SNUM Text"[/FONT]
 
I commented out the existing code and inserted your line.

I ran a couple of imports and No Error.

Thank you. Your input is greatly appreciated.

Regards,

Jim
 

Users who are viewing this thread

Back
Top Bottom