Solved

MS Access ADO open question

Posted on 2010-09-21
2
530 Views
Last Modified: 2012-05-10
I have the following code in an Access 2007 DB.  When I get to the line that is supposed to open rstIW I get an error that states:
Run time error 91
Object variable or With Block variable not set.

I tried creating a second connection called cnnLocal2 but that didn't work, I got the same error.
It's probably a rookie mistake (because I am one) but I can't see it.  What am I doing wrong?

Sub Extract_Raw_Data()
    Dim cnnLocal As New ADODB.Connection
    Dim rstExcel As New ADODB.Recordset
    Dim rstIW As ADODB.Recordset
    Dim IWType As String
    Dim IWSubType As String
    Set cnnLocal = CurrentProject.Connection
    rstExcel.Open "Select * from ImageWires", cnnLocal, adOpenStatic, adLockPessimistic
    rstIW.Open "Select * from tblIW WHERE 1 = 0", cnnLocal, adOpenDynamic, adLockOptimistic
   
    While Not rstExcel.EOF
        If rstExcel.Fields("F2") = "Pre-Labeled ImageWires 1.5" Or _
           rstExcel.Fields("F2") = "Pre-Labeled ImageWires 1.8" Or _
           rstExcel.Fields("F2") = "Pre-Labeled C7 Dragonfly" Then
            IWType = rstExcel.Fields("F2")
        End If
        If (rstExcel.Fields("F2") = "12424-00" Or _
           rstExcel.Fields("F2") = "12513-00" Or _
           rstExcel.Fields("F2") = "13751-00") And _
           rstExcel.Fields("F4") > " " Then   'Use the part # to indicate data rec and Lot # to show data present
            rstIW.AddNew
            rstIW.Fields("IW PN") = rstExcel.Fields("F2")
            rstIW.Fields("IW Lot") = rstExcel.Fields("F4")
            rstIW.Fields("IW Qty") = rstExcel.Fields("F6")
            If IsNumeric(rstExcel.Fields("F8")) Then
                rstIW.Fields("IW Exp_Date") = rstExcel.Fields("F8")
                rstIW.Fields("IW Status") = Null
            Else
                rstIW.Fields("IW Exp_Date") = Null
                rstIW.Fields("IW Status") = rstExcel.Fields("F8")
            End If
            rstIW.Fields("IW Type") = IWType
            rstIW.Update
        End If
       
        rstExcel.MoveNext
    Wend
    rstExcel.Close
End Sub

0
Comment
Question by:RichNH
2 Comments
 
LVL 6

Expert Comment

by:UsamaFoad
ID: 33730419
I think the problem in:

IWType = rstExcel.Fields("F2")

You set the "IWType" in the first IF statement but you do not if this condition has not been achieved. So in line 32 (rstIW.Fields("IW Type") = IWType) the "IWType" generate the error. You need to set "IWType" before the second "IF".
Also I think nesting the first "IF" with the other "IF"s will be better.


Sub Extract_Raw_Data()

    Dim cnnLocal As New ADODB.Connection

    Dim rstExcel As New ADODB.Recordset

    Dim rstIW As ADODB.Recordset

    Dim IWType As String

    Dim IWSubType As String

    Set cnnLocal = CurrentProject.Connection

    rstExcel.Open "Select * from ImageWires", cnnLocal, adOpenStatic, adLockPessimistic

    rstIW.Open "Select * from tblIW WHERE 1 = 0", cnnLocal, adOpenDynamic, adLockOptimistic

    

    While Not rstExcel.EOF

        If rstExcel.Fields("F2") = "Pre-Labeled ImageWires 1.5" Or _

           rstExcel.Fields("F2") = "Pre-Labeled ImageWires 1.8" Or _

           rstExcel.Fields("F2") = "Pre-Labeled C7 Dragonfly" Then

            IWType = rstExcel.Fields("F2")

        else

			IWType = ' =============================You need to set IWType here======================

			If (rstExcel.Fields("F2") = "12424-00" Or _

				rstExcel.Fields("F2") = "12513-00" Or _

				rstExcel.Fields("F2") = "13751-00") And _

				rstExcel.Fields("F4") > " " Then   'Use the part # to indicate data rec and Lot # to show data present

				rstIW.AddNew

				rstIW.Fields("IW PN") = rstExcel.Fields("F2")

				rstIW.Fields("IW Lot") = rstExcel.Fields("F4")

				rstIW.Fields("IW Qty") = rstExcel.Fields("F6")

				If IsNumeric(rstExcel.Fields("F8")) Then

					rstIW.Fields("IW Exp_Date") = rstExcel.Fields("F8")

					rstIW.Fields("IW Status") = Null

				Else

					rstIW.Fields("IW Exp_Date") = Null

					rstIW.Fields("IW Status") = rstExcel.Fields("F8")

				End If

				rstIW.Fields("IW Type") = IWType

				rstIW.Update

			End If

        End If

        rstExcel.MoveNext

    Wend

    rstExcel.Close

End Sub

Open in new window

0
 
LVL 1

Accepted Solution

by:
RichNH earned 0 total points
ID: 33743307
Thank you for the response.  Actually I thought this note didn't go in.  I figured it out shortly after I finished typing it.  It was that I did not dim rstIW as a NEW ADODB.Recordset.  Once I put the New in there the code worked fine.

Regarduing the specific code.  The 1st If is a standalone.  The value only occurs on the first record (I'm reading an Excel Workbook that the inventory manager uses to manage consumables in our company).  Because the title (value) only occurs on the first record the IF has to be standalone as it will not be true on the 2nd - nth records.  I'm simply setting a temp value with the title of the data I'm about to read.

Thank you again.

Rich
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now