473,666 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why am I getting an error 91 - Object variable or With block variable not set

5 New Member
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

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdImport_Click()
  2. 'Purpose:  To import customer receivings from the pre-formatted receiving spreadsheet
  3. 'Description:
  4.  
  5. On Error GoTo Err_cmdImport_Click
  6.  
  7.     'Set all necessary variables
  8.     Dim conReceive As New ADODB.Connection
  9.     Dim rstReceiveData As ADODB.Recordset
  10.     Dim strSheet As String, strQuery As String, strSrcField As String, strDesField As String
  11.     Dim strDesName As String, strSrcName As String, strImportDir As String
  12.     Dim strMessage As String, strGrouping As String
  13.     Dim dbsPDSCWD As Database
  14.     Dim rstImportSchema As Recordset, rstReceiveHeader As Recordset, rstReceiveDetail As Recordset, rstItems As Recordset
  15.     Dim retValue As Variant
  16.  
  17.     Set dbsPDSCWD = CurrentDb
  18.  
  19.     DoCmd.Hourglass True
  20.     'Intialize variables
  21.     Me!txtStatus = "Intializing variables..."
  22.     Me!prbStatus.Value = 10
  23.     Me.Repaint
  24.     strImportDir = DLookup("txtCustDirName", "tblAllCustomers", "pkeyCustNo = '" & Me!cboCustNo & "'")
  25.     strSheet = "Receiving$"
  26.  
  27.     'Create recordsets for the Schema, Header, and Detail tables
  28.     strQuery = "SELECT * FROM tblSchema WHERE txtSchCustNo = '" & Me!cboCustNo & "' AND txtSchType = 'Receiving'"
  29.     Set rstImportSchema = dbsPDSCWD.OpenRecordset(strQuery, dbOpenDynaset, dbReadOnly)
  30.     Set rstReceiveHeader = dbsPDSCWD.OpenRecordset("tblOEReceivingHeader", dbOpenDynaset)
  31.     Set rstReceiveDetail = dbsPDSCWD.OpenRecordset("tblOEReceive", dbOpenDynaset)
  32.     Set rstItems = dbsPDSCWD.OpenRecordset("tblOEItems", dbOpenDynaset)
  33.  
  34.     'Create the connection to the receiving Excel file
  35.     Me!txtStatus = "Connecting to source file..."
  36.     Me!prbStatus.Value = 20
  37.     Me.Repaint
  38.     With conReceive
  39.         .Provider = "Microsoft.Jet.OLEDB.4.0"
  40.         .Properties("Extended Properties").Value = "Excel 8.0;IMEX=1"
  41.         .Open strImportDir & Me!cboCustNo & "Receiving.xls"
  42.     End With
  43.  
  44.     strQuery = "SELECT * FROM tblSchema WHERE txtSchCustNo = '" & Me!cboCustNo & "' AND txtSchType = 'Items'"
  45.     Set rstImportSchema = dbsPDSCWD.OpenRecordset(strQuery, dbOpenDynaset, dbReadOnly)
  46.  
  47.     'Build the SQL string for the summarized list of items
  48.     Me!txtStatus = "Creating list of items..."
  49.     Me!prbStatus.Value = 30
  50.     Me.Repaint
  51.     strQuery = "SELECT "
  52.     rstImportSchema.MoveFirst
  53.     Do While Not rstImportSchema.EOF
  54.         If rstImportSchema!txtSchSrcFieldName = "ItemNo" Then
  55.             strQuery = strQuery & strSheet & "." & rstImportSchema!txtSchSrcFieldName & ", "
  56.             strGrouping = "GROUP BY " & rstImportSchema!txtSchSrcFieldName
  57.         Else
  58.             strQuery = strQuery & "First(" & strSheet & "." & rstImportSchema!txtSchSrcFieldName & ") AS " & rstImportSchema!txtSchSrcFieldName & ", "
  59.         End If
  60.         rstImportSchema.MoveNext
  61.     Loop
  62.  
  63.     strQuery = Left(strQuery, Len(strQuery) - 2)
  64.     strQuery = strQuery & " FROM [" & strSheet & "] " & strGrouping
  65.     Set rstReceiveData = conReceive.Execute(strQuery)
  66.  
  67.     rstImportSchema.MoveFirst
  68.     rstReceiveData.MoveFirst
  69.  
  70. On Error Resume Next
  71.  
  72.     'Add new items to items table
  73.     Me!txtStatus = "Adding new items to database..."
  74.     Me!prbStatus.Value = 40
  75.     Me.Repaint
  76.     Do While Not rstReceiveData.EOF
  77.         With rstItems
  78.             .AddNew
  79.             Do While Not rstImportSchema.EOF
  80.                 strDesName = rstImportSchema!txtSchDesFieldName
  81.                 strSrcName = rstImportSchema!txtSchSrcFieldName
  82.                 .Fields(strDesName).Value = rstReceiveData.Fields(strSrcName).Value
  83.                 rstImportSchema.MoveNext
  84.             Loop
  85.             !pkeyItemCustNo = Me!cboCustNo
  86.             !txtItemUser = TempVars!CurrentUser
  87.             !txtItemLoc = "Not assigned"
  88.             !dtmItemDateTime = Now()
  89.             !ysnItemObs = False
  90.             .Update
  91.         End With
  92.         rstImportSchema.MoveFirst
  93.         rstReceiveData.MoveNext
  94.     Loop
  95.  
  96.     rstItems.Close
  97.  
  98. On Error GoTo Err_cmdImport_Click
  99.  
  100.     'Add receiving header record
  101.     Me!txtStatus = "Adding header records..."
  102.     Me!prbStatus.Value = 50
  103.     Me.Repaint
  104.     strQuery = "SELECT * FROM tblSchema WHERE txtSchCustNo = '" & Me!cboCustNo & "' AND txtSchType = 'Receiving' " & _
  105.     "AND txtSchFormat = 'Header'"
  106.     Set rstImportSchema = dbsPDSCWD.OpenRecordset(strQuery, dbOpenDynaset, dbReadOnly)
  107.  
  108.     'Build strQuery for summarized list of headers
  109.     strQuery = "SELECT "
  110.     rstImportSchema.MoveFirst
  111.     Do While Not rstImportSchema.EOF
  112.         If rstImportSchema!txtSchSrcFieldName = "ReceivingNo" Then
  113.             strQuery = strQuery & strSheet & "." & rstImportSchema!txtSchSrcFieldName & ", "
  114.             strGrouping = "GROUP BY " & rstImportSchema!txtSchSrcFieldName
  115.         Else
  116.             strQuery = strQuery & "First(" & strSheet & "." & rstImportSchema!txtSchSrcFieldName & ") AS " & rstImportSchema!txtSchSrcFieldName & ", "
  117.         End If
  118.         rstImportSchema.MoveNext
  119.     Loop
  120.  
  121.     strQuery = Left(strQuery, Len(strQuery) - 2)
  122.     strQuery = strQuery & " FROM [" & strSheet & "] " & strGrouping
  123.     Debug.Print strQuery
  124.     Set rstReceiveData = conReceive.Execute(strQuery)
  125.  
  126.     rstImportSchema.MoveFirst
  127.     rstReceiveData.MoveFirst
  128.  
  129.     Do While Not rstReceiveData.EOF
  130.         If IsNull(rstReceiveData.Fields("ReceivingNo").Value) Then
  131.         Else
  132.             With rstReceiveHeader
  133.                 .AddNew
  134.                 Do While Not rstImportSchema.EOF
  135.                     strDesName = rstImportSchema!txtSchDesFieldName
  136.                     strSrcName = rstImportSchema!txtSchSrcFieldName
  137.                     .Fields(strDesName).Value = rstReceiveData.Fields(strSrcName).Value
  138.                     rstImportSchema.MoveNext
  139.                 Loop
  140.                 !pkeyRecvHdCustNo = Me!cboCustNo
  141.                 !txtRecvHdUserID = TempVars!CurrentUser
  142.                 !dtmRecvHdDateTimeStamp = Now()
  143.                 !intRecvHdStat = 1
  144.                 .Update
  145.             End With
  146.         End If
  147.             rstImportSchema.MoveFirst
  148.             rstReceiveData.MoveNext
  149.     Loop
  150.  
  151.     rstReceiveData.Close
  152.     rstReceiveHeader.Close
  153.     rstImportSchema.Close
  154.  
  155.     'Add the detail records
  156.     Me!txtStatus = "Adding detail records..."
  157.     Me!prbStatus.Value = 60
  158.     Me.Repaint
  159.     strQuery = "SELECT * FROM [" & strSheet & "]"
  160.     Set rstReceiveData = conReceive.Execute(strQuery)
  161.  
  162.     strQuery = "SELECT * FROM tblSchema WHERE txtSchCustNo = '" & Me!cboCustNo & "' AND txtSchType = 'Receiving' " & _
  163.     "AND txtSchFormat = 'Detail'"
  164.     Set rstImportSchema = dbsPDSCWD.OpenRecordset(strQuery, dbOpenDynaset, dbReadOnly)
  165.  
  166.     rstImportSchema.MoveFirst
  167.     rstReceiveData.MoveFirst
  168.  
  169.     Do While Not rstReceiveData.EOF
  170.         If IsNull(rstReceiveData.Fields("ItemNo")) Then
  171.         Else
  172.             With rstReceiveDetail
  173.                 .AddNew
  174.                 Do While Not rstImportSchema.EOF
  175.                     strDesName = rstImportSchema!txtSchDesFieldName
  176.                     strSrcName = rstImportSchema!txtSchSrcFieldName
  177.                     .Fields(strDesName).Value = rstReceiveData.Fields(strSrcName).Value
  178.                     rstImportSchema.MoveNext
  179.                 Loop
  180.                 !txtRecvCustNo = Me!cboCustNo
  181.                 !txtRecvUser = TempVars!CurrentUser
  182.                 !dtmRecvDateTimeStamp = Now()
  183.                 .Update
  184.             End With
  185.         End If
  186.                 rstImportSchema.MoveFirst
  187.                 rstReceiveData.MoveNext
  188.                 intRecordCount = intRecordCount + 1
  189.     Loop
  190.  
  191.     Me!txtStatus = "Import Complete!"
  192.     Me!prbStatus.Value = 70
  193.     Me.Repaint
  194.  
  195.     rstReceiveData.Close
  196.     rstReceiveDetail.Close
  197.     conReceive.Close
  198.  
  199. Exit_cmdImport_Click:
  200.     DoCmd.Hourglass False
  201.     DoCmd.SetWarnings True
  202.     Exit Sub
  203.  
  204. Err_cmdImport_Click:
  205.     Select Case Err
  206.         Case 3022
  207.             strMessage = "Receiving number " & rstReceiveHeader!pkeyRecvHdNo & " already exists for this customer.  " & _
  208.             "The same receiving number cannot be used for a customer twice.  Please correct the receiving number " & _
  209.             "and re-import.  Receiving has not be imported."
  210.             MsgBox strMessage, vbOKOnly + vbCritical, "Import Error"
  211.             Me!txtStatus = "Import failed!"
  212.             Me!prbStatus.Value = 70
  213.             Me.Repaint
  214.             Resume Exit_cmdImport_Click
  215.         Case -2147467259
  216.             Resume Next
  217.         Case Else
  218.             MsgBox Err & " - " & Err.Description
  219.             strActivity = ActivityLog("System error", Me.Name, Err & " - " & Err.Description)
  220.             Me!txtStatus = "Import failed!"
  221.             Me!prbStatus.Value = 70
  222.             Me.Repaint
  223.             Resume Exit_cmdImport_Click
  224.     End Select
  225.  
  226. End Sub
Jan 5 '11 #1
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.
Expand|Select|Wrap|Line Numbers
  1. MsgBox IIf(Not rstReceiveData Is Nothing, "Recordset exists", "Recordset does not exist")
Jan 6 '11 #2
Brad Nerbovig
5 New Member
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...
Jan 6 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
15300
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 -------------------------------------------------------------------
0
2054
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.
3
35503
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
1
2298
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: ...
8
18423
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
2
3421
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, _...
3
3252
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...
4
3629
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
5
3250
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
0
377
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...
0
8356
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,...
1
8551
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,...
0
7386
agi2029
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...
1
6198
isladogs
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...
0
5664
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();...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
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
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.