I am getting this error when connecting to an Excel file from Access 2010. The weird thing is, if I click on okay when the routing fails, and then run it again it works just fine. In fact, if I run it again anytime after I have gotten this error it works just fine. The only time the error crops up is the first time I run the routine after opening up the database. There's a lot of code in this routine which I'll post, but the error occurs at line 65 with the first "Set rstReceiveData = conReceive.Exec ute(strQuery)" statement which is the first time I execute a query against the Excel file. Also, don't laugh at my code. I'm an amatuer. ;P - Private Sub cmdImport_Click()
-
'Purpose: To import customer receivings from the pre-formatted receiving spreadsheet
-
'Description:
-
-
On Error GoTo Err_cmdImport_Click
-
-
'Set all necessary variables
-
Dim conReceive As New ADODB.Connection
-
Dim rstReceiveData As ADODB.Recordset
-
Dim strSheet As String, strQuery As String, strSrcField As String, strDesField As String
-
Dim strDesName As String, strSrcName As String, strImportDir As String
-
Dim strMessage As String, strGrouping As String
-
Dim dbsPDSCWD As Database
-
Dim rstImportSchema As Recordset, rstReceiveHeader As Recordset, rstReceiveDetail As Recordset, rstItems As Recordset
-
Dim retValue As Variant
-
-
Set dbsPDSCWD = CurrentDb
-
-
DoCmd.Hourglass True
-
'Intialize variables
-
Me!txtStatus = "Intializing variables..."
-
Me!prbStatus.Value = 10
-
Me.Repaint
-
strImportDir = DLookup("txtCustDirName", "tblAllCustomers", "pkeyCustNo = '" & Me!cboCustNo & "'")
-
strSheet = "Receiving$"
-
-
'Create recordsets for the Schema, Header, and Detail tables
-
strQuery = "SELECT * FROM tblSchema WHERE txtSchCustNo = '" & Me!cboCustNo & "' AND txtSchType = 'Receiving'"
-
Set rstImportSchema = dbsPDSCWD.OpenRecordset(strQuery, dbOpenDynaset, dbReadOnly)
-
Set rstReceiveHeader = dbsPDSCWD.OpenRecordset("tblOEReceivingHeader", dbOpenDynaset)
-
Set rstReceiveDetail = dbsPDSCWD.OpenRecordset("tblOEReceive", dbOpenDynaset)
-
Set rstItems = dbsPDSCWD.OpenRecordset("tblOEItems", dbOpenDynaset)
-
-
'Create the connection to the receiving Excel file
-
Me!txtStatus = "Connecting to source file..."
-
Me!prbStatus.Value = 20
-
Me.Repaint
-
With conReceive
-
.Provider = "Microsoft.Jet.OLEDB.4.0"
-
.Properties("Extended Properties").Value = "Excel 8.0;IMEX=1"
-
.Open strImportDir & Me!cboCustNo & "Receiving.xls"
-
End With
-
-
strQuery = "SELECT * FROM tblSchema WHERE txtSchCustNo = '" & Me!cboCustNo & "' AND txtSchType = 'Items'"
-
Set rstImportSchema = dbsPDSCWD.OpenRecordset(strQuery, dbOpenDynaset, dbReadOnly)
-
-
'Build the SQL string for the summarized list of items
-
Me!txtStatus = "Creating list of items..."
-
Me!prbStatus.Value = 30
-
Me.Repaint
-
strQuery = "SELECT "
-
rstImportSchema.MoveFirst
-
Do While Not rstImportSchema.EOF
-
If rstImportSchema!txtSchSrcFieldName = "ItemNo" Then
-
strQuery = strQuery & strSheet & "." & rstImportSchema!txtSchSrcFieldName & ", "
-
strGrouping = "GROUP BY " & rstImportSchema!txtSchSrcFieldName
-
Else
-
strQuery = strQuery & "First(" & strSheet & "." & rstImportSchema!txtSchSrcFieldName & ") AS " & rstImportSchema!txtSchSrcFieldName & ", "
-
End If
-
rstImportSchema.MoveNext
-
Loop
-
-
strQuery = Left(strQuery, Len(strQuery) - 2)
-
strQuery = strQuery & " FROM [" & strSheet & "] " & strGrouping
-
Set rstReceiveData = conReceive.Execute(strQuery)
-
-
rstImportSchema.MoveFirst
-
rstReceiveData.MoveFirst
-
-
On Error Resume Next
-
-
'Add new items to items table
-
Me!txtStatus = "Adding new items to database..."
-
Me!prbStatus.Value = 40
-
Me.Repaint
-
Do While Not rstReceiveData.EOF
-
With rstItems
-
.AddNew
-
Do While Not rstImportSchema.EOF
-
strDesName = rstImportSchema!txtSchDesFieldName
-
strSrcName = rstImportSchema!txtSchSrcFieldName
-
.Fields(strDesName).Value = rstReceiveData.Fields(strSrcName).Value
-
rstImportSchema.MoveNext
-
Loop
-
!pkeyItemCustNo = Me!cboCustNo
-
!txtItemUser = TempVars!CurrentUser
-
!txtItemLoc = "Not assigned"
-
!dtmItemDateTime = Now()
-
!ysnItemObs = False
-
.Update
-
End With
-
rstImportSchema.MoveFirst
-
rstReceiveData.MoveNext
-
Loop
-
-
rstItems.Close
-
-
On Error GoTo Err_cmdImport_Click
-
-
'Add receiving header record
-
Me!txtStatus = "Adding header records..."
-
Me!prbStatus.Value = 50
-
Me.Repaint
-
strQuery = "SELECT * FROM tblSchema WHERE txtSchCustNo = '" & Me!cboCustNo & "' AND txtSchType = 'Receiving' " & _
-
"AND txtSchFormat = 'Header'"
-
Set rstImportSchema = dbsPDSCWD.OpenRecordset(strQuery, dbOpenDynaset, dbReadOnly)
-
-
'Build strQuery for summarized list of headers
-
strQuery = "SELECT "
-
rstImportSchema.MoveFirst
-
Do While Not rstImportSchema.EOF
-
If rstImportSchema!txtSchSrcFieldName = "ReceivingNo" Then
-
strQuery = strQuery & strSheet & "." & rstImportSchema!txtSchSrcFieldName & ", "
-
strGrouping = "GROUP BY " & rstImportSchema!txtSchSrcFieldName
-
Else
-
strQuery = strQuery & "First(" & strSheet & "." & rstImportSchema!txtSchSrcFieldName & ") AS " & rstImportSchema!txtSchSrcFieldName & ", "
-
End If
-
rstImportSchema.MoveNext
-
Loop
-
-
strQuery = Left(strQuery, Len(strQuery) - 2)
-
strQuery = strQuery & " FROM [" & strSheet & "] " & strGrouping
-
Debug.Print strQuery
-
Set rstReceiveData = conReceive.Execute(strQuery)
-
-
rstImportSchema.MoveFirst
-
rstReceiveData.MoveFirst
-
-
Do While Not rstReceiveData.EOF
-
If IsNull(rstReceiveData.Fields("ReceivingNo").Value) Then
-
Else
-
With rstReceiveHeader
-
.AddNew
-
Do While Not rstImportSchema.EOF
-
strDesName = rstImportSchema!txtSchDesFieldName
-
strSrcName = rstImportSchema!txtSchSrcFieldName
-
.Fields(strDesName).Value = rstReceiveData.Fields(strSrcName).Value
-
rstImportSchema.MoveNext
-
Loop
-
!pkeyRecvHdCustNo = Me!cboCustNo
-
!txtRecvHdUserID = TempVars!CurrentUser
-
!dtmRecvHdDateTimeStamp = Now()
-
!intRecvHdStat = 1
-
.Update
-
End With
-
End If
-
rstImportSchema.MoveFirst
-
rstReceiveData.MoveNext
-
Loop
-
-
rstReceiveData.Close
-
rstReceiveHeader.Close
-
rstImportSchema.Close
-
-
'Add the detail records
-
Me!txtStatus = "Adding detail records..."
-
Me!prbStatus.Value = 60
-
Me.Repaint
-
strQuery = "SELECT * FROM [" & strSheet & "]"
-
Set rstReceiveData = conReceive.Execute(strQuery)
-
-
strQuery = "SELECT * FROM tblSchema WHERE txtSchCustNo = '" & Me!cboCustNo & "' AND txtSchType = 'Receiving' " & _
-
"AND txtSchFormat = 'Detail'"
-
Set rstImportSchema = dbsPDSCWD.OpenRecordset(strQuery, dbOpenDynaset, dbReadOnly)
-
-
rstImportSchema.MoveFirst
-
rstReceiveData.MoveFirst
-
-
Do While Not rstReceiveData.EOF
-
If IsNull(rstReceiveData.Fields("ItemNo")) Then
-
Else
-
With rstReceiveDetail
-
.AddNew
-
Do While Not rstImportSchema.EOF
-
strDesName = rstImportSchema!txtSchDesFieldName
-
strSrcName = rstImportSchema!txtSchSrcFieldName
-
.Fields(strDesName).Value = rstReceiveData.Fields(strSrcName).Value
-
rstImportSchema.MoveNext
-
Loop
-
!txtRecvCustNo = Me!cboCustNo
-
!txtRecvUser = TempVars!CurrentUser
-
!dtmRecvDateTimeStamp = Now()
-
.Update
-
End With
-
End If
-
rstImportSchema.MoveFirst
-
rstReceiveData.MoveNext
-
intRecordCount = intRecordCount + 1
-
Loop
-
-
Me!txtStatus = "Import Complete!"
-
Me!prbStatus.Value = 70
-
Me.Repaint
-
-
rstReceiveData.Close
-
rstReceiveDetail.Close
-
conReceive.Close
-
-
Exit_cmdImport_Click:
-
DoCmd.Hourglass False
-
DoCmd.SetWarnings True
-
Exit Sub
-
-
Err_cmdImport_Click:
-
Select Case Err
-
Case 3022
-
strMessage = "Receiving number " & rstReceiveHeader!pkeyRecvHdNo & " already exists for this customer. " & _
-
"The same receiving number cannot be used for a customer twice. Please correct the receiving number " & _
-
"and re-import. Receiving has not be imported."
-
MsgBox strMessage, vbOKOnly + vbCritical, "Import Error"
-
Me!txtStatus = "Import failed!"
-
Me!prbStatus.Value = 70
-
Me.Repaint
-
Resume Exit_cmdImport_Click
-
Case -2147467259
-
Resume Next
-
Case Else
-
MsgBox Err & " - " & Err.Description
-
strActivity = ActivityLog("System error", Me.Name, Err & " - " & Err.Description)
-
Me!txtStatus = "Import failed!"
-
Me!prbStatus.Value = 70
-
Me.Repaint
-
Resume Exit_cmdImport_Click
-
End Select
-
-
End Sub
2 3708 ADezii 8,834
Recognized Expert Expert
Just for curiosity, insert this Line after Code Line# 65 to see if the Recordset actually exists after the Initial Opening of the DB. - MsgBox IIf(Not rstReceiveData Is Nothing, "Recordset exists", "Recordset does not exist")
I would, except the error occurs at line 65. If it gets past that line, which it does after it has errored out the first time, then everything is groovy. Inserting the line of code you have above returns the "Recordset exists" msgbox the second time I run the routine. It never gets there on the first run.
For some reason, it is not recognizing the connection to the Excel file when I run through it the first time. I'm wondering if there's a library or reference that is not getting loaded properly until it goes through the routine a second time...
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Joe Schwartz |
last post by:
All,
Could somebody please help me out here. I am getting an "Object
variable or With block variable not set" error with the following
code. This happens when I enter a value into the "ponum" control on my
"frm_queries" form. I appreciate the help.
Joe
-------------------------------------------------------------------
|
by: RJN |
last post by:
Hi
Sorry for posting again. I'm calling a shared method in the class. The
following error is found in production though I'm not able to reproduce.
Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o,
Type objType, String name, Object args, String paramnames, Boolean
CopyBack)
Object variable or With block variable not set.
|
by: Richard Hollenbeck |
last post by:
I've marked the line in this subroutine where I've been getting this error.
It may be something stupid but I've been staring at this error trying to fix
it for over an hour. I'm pretty sure the table and field names and controls
are all named correctly, and the control referred to in the errant code is
open, and it has data in it.
Private Sub cmdAddIngredientToRecipe_Click()
' Get RecipeID for future action query
Dim recipeID As Long
|
by: abhijmenbumca07 |
last post by:
Object variable or With block variable not set.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object variable or With block variable not set.
Source Error:
Line 305: Me.txt_amount.Text = mat(0)
Line 306: ...
|
by: sandy21380 |
last post by:
I keep getting the error 'Object variable or With block variable not set' with the following code. 'i' is integer, ViewOptions is OptionBox.
Any idea what I'm doing wrong?
For i = 0 To 4
If ViewOptions.Value = 1 Then
If i = 0 Then
If IsNumeric(strCriteria(i)) Then
LinkCriteriaFinal = "(" & LinkCriteria(i) & " like " & strCriteria(i) & " &'*' )"
Else
| |
by: confused99 |
last post by:
Hi everybody... I've been working on moving access data to a word document for the last 10 days but keep getting stuck. The latest problem is that i'm getting the error "Object Variable Or With Block Variable Not Set". The code is as follows.
Dim objWord As New Word.Document
Set objWord=GetObject("C:\Abc.doc")
objWord.Application.Visible=True
objWord.MailMerge.OpenDataSource _
Name:=CurrentDb.Name, _
LinkToSource:=True, _...
|
by: Newbie19 |
last post by:
I'm trying to get a list of all subfolders in a folder on a share drive, but I keep on getting this error message:
Object variable or With block variable not set.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object variable or With block...
|
by: sahitya sidda |
last post by:
"Error 91: object variable or with block variable not set"
I am getting the above error while retrieving the index of selected row in listview.
Please help me out
Private Sub ListPersons_DblClick()
Dim row As Integer
Dim item As ListItem
|
by: Al G |
last post by:
Hi,
I'm converting a bit of POP3 VB6 code to VB2005, and have run into this
error with the following code.
Can someone help me find out what I'm missing/doing wrong?
'holds the attachments
Class attachmentBlockParameter
|
by: David C |
last post by:
I am getting this error in the Databound event of a GridView and I am having
a difficult time debugging. I do not get the error on every record that I
edit in the GridView. The error is occurring on the lines that start with
varCtl. (for example varCtl.CssClass = "Hide"). The other thing that is
strange is that it occurs when I click the Edit button on the GridView and
yet the error is pointing to a line in the code that only gets...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |