[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1105
  • Last Modified:

Object Variable or With block variable not set

Hello~
When trying to execute this block of code below I am getting the Run-time error '91':
Object Variable or With block variable not set. Tried to change it to Provider=Microsoft.Ace.OLEDB.12.0;" & _ and that gave the same error. Anyone have any suggestions?
Thank you!



Private Sub ServerDBConnection()
    ' Procedure is used to create the connection to the server database
    Dim conn As ADODB.Connection
    Dim sPath As String
    Dim sPW As String
    
    
    sPath = "\\fr01apthr001\HRData\HRIS\App\pcdata.accdb"
    sPW = "jb007"
    
    conn.ConnectionString = _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & sPath & ";" & _
        "Jet OLEDB:Database Password=" & sPW
    conn.Open
    
    '  display the last process time
    Me.txtLastTime.Value = sysProcessTime(sysProcess, Now(), "sysRetrieve")
End Sub

Open in new window

0
dminx13
Asked:
dminx13
  • 8
  • 6
  • 2
  • +1
1 Solution
 
mvidasCommented:
dminx,

You have to initialize the conn variable. Just change your variable declaration to
Dim conn As New ADODB.Connection

Open in new window

Matt
0
 
mvidasCommented:
I should also mention that if you don't want to initialize it there, you can always do it separately:
Set conn = New ADODB.Connection

Open in new window

As long as that is above where you're using it (when setting the connection string) you'll be fine.
0
 
dminx13Author Commented:
OK that worked but now it's throwing the same error on a different place.  This process is supposed to delete existing tables and recreate them with the newest information. It doesn't appear like it is picking up the external data that it needs to in the SQLStatement it is just empty....

Set rs = ServerDB.OpenRecordset(SQLStatement) the SQLStatement is "JCC" which is a table in the database that this process needs to update.


Here is the full code with the line that is offending:
Private Function ExportRecords(SQLStatement As String, ExternalRecords As Recordset) As Long
    '  Procedure is used to export the JCC to the PeopleCenter Database
    Dim db As Database
    Dim rs As Recordset
    Dim sSQL As String
    Dim sProg As String
    Dim fld As DAO.Field
    Dim i As Long
    
    
    '  Look for data in external databases and create the recordset as needed
    Select Case SQLStatement
        Case "Applicants"                           '  Applicant database information
            Set rs = CreateApplicantDBRecordset(SQLStatement)
        Case "Hires"                                '  Applicant DB information
            Set rs = CreateApplicantDBRecordset(SQLStatement)
        Case "Requisitions"                         '  Requisition records
            Set rs = CreateRequisitionRecordset(SQLStatement)
        Case "Contract_Staff"
            Set rs = CreateContractRecordset(SQLStatement)
        '  Used to be for contract positions - replaced with actual positions
        'Case "Positions"
        '    Set rs = CreateContractRecordset(SQLStatement)
        Case Else   '  Recordset can be found in the Server database
            Set rs = ServerDB.OpenRecordset(SQLStatement)    '<--------
    End Select
    
    i = 0
    With rs
        If .EOF And .BOF Then
            '  do nothing
        Else
            .MoveLast
            .MoveFirst
            Do
                i = i + 1
                sProg = "Exporting " + CStr(i) + " of " + CStr(.RecordCount) + " records"
                Me.txtCurrent.Value = sProg
                DoEvents
                    ExternalRecords.AddNew
                        For Each fld In ExternalRecords.Fields
                            If IsNull(.Fields(fld.Name)) Then
                                '  Do Nothing
                            Else
                                ExternalRecords.Fields(fld.Name) = .Fields(fld.Name)
                            End If
                        Next fld
                    ExternalRecords.Update
                .MoveNext
            Loop Until .EOF
            
        End If
    End With
    
    ExportRecords = i
    Set rs = Nothing
    Set db = Nothing
End Function

Open in new window

Option-Compare-Database.docx
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mvidasCommented:
Where does the ServerDB variable come from? I don't see it dimensioned or passed at all.
0
 
dminx13Author Commented:
It is in the Option Compare section as well as the ServerDBConnection code in the word document that I attached.
0
 
mvidasCommented:
I see now. It looks like in the subroutine ServerDBConnection you have this line commented out:
    'Set ServerDB = wsp.OpenDatabase(sPath, False, False, sPW)

Open in new window

This was the line that initialized the variable and sets it to reference your server database. Why did you comment it out?
0
 
dminx13Author Commented:
Good question.  Under what section is that? I know that when we updated from 2003 some of the wsp lines went away because of the conn lines.
0
 
mvidasCommented:
It was in the original Sub you posted about, just commented out (I assumed you removed that before posting on EE for easier readability). Line 23 below:
Private Sub ServerDBConnection()
    ' Procedure is used to create the connection to the server database
    'Dim wsp As Workspace
    Dim conn As New ADODB.Connection
    Dim sPath As String
    Dim sPW As String
    
    
    ''''sPath = GetSystemData("datafile")
    ''''sPW = ";PWD=" + GetSystemData("password")
    sPath = "\\fr01apthr001\HRData\HRIS\App\pcdata.accdb"
    sPW = "jb007"
    
    conn.ConnectionString = _
        "Provider=Microsoft.Ace.OLEDB.12.0;" & _
        "Data Source=" & sPath & ";" & _
        "Jet OLEDB:Database Password=" & sPW
    conn.Open
    'sPath = "I:\Users\HRD\HRIS\App\TestDB\pcdata.mdb"
    'sPW = "jb007"
    
    'Set wsp = DBEngine.Workspaces(0)
    'Set ServerDB = wsp.OpenDatabase(sPath, False, False, sPW)
    
    '  display the last process time
    Me.txtLastTime.Value = sysProcessTime(sysProcess, Now(), "sysRetrieve")
End Sub

Open in new window

If you're not going to go that route, maybe in the function "ExportRecords" where you're getting the error, you could use something like
 Set rs = New Recordset
 rs.Open SQLStatement, conn, adOpenKeyset, adLockReadOnly

Open in new window

Or however you want to use it. Since it seems you're converting all your old methods into ADODB, that would be one way to do it. I am not a database expert, so its possible there are better methods than this.
0
 
dminx13Author Commented:
We were only trying to convert what we HAD to. That is why the dim wsp was commented out as well as the other line. I tried adding those back in and that created different errors. Will get back into this more tomorrow and try what you've suggested.
0
 
dminx13Author Commented:
Not sure what to do with this post at this point. I don't have the expertise to traslate into code some of the suggestions and at this point I just linked tables into the database that I could not update as a workaround. So I can get the data that I need, just not using what was suggested. I suppose I can let this expire as some of the posts above may be a solution, I just can't translate into viable code to test them.
0
 
mvidasCommented:
I've asked for assistance from some other experts here who can hopefully help you transition it better. Stay patient please :)
0
 
aikimarkCommented:
You should not use a recordset object to execute (Delete, Insert, or Update) statements.  Use any of the following:
* dbEngine(0)(0).Execute()
* your connection object
* DoCmd.RunQuery (or similar command)
* CurrentDb.Execute()
0
 
Vadim RappCommented:
If you have limited coding experience, consider hiring someone, such as Top Experts you can see on the right side of the page. Many have their contact information in their profiles and will be happy to help on very reasonable terms. I've been on both sides and can almost guarantee that you won't be sorry.
0
 
dminx13Author Commented:
aikimark:
Can you be more specific? Like replace this line of code with any of these lines of code and try.

vadimrapp1:
I'd love to but we don't have the money. We just spent $$ on a contractor for a limited engagement to convert the database and we had only 200 hours. So at this point I am on my own to complete what the contractor did not :-(
0
 
dminx13Author Commented:
So in starting fresh and re-coping the code from the original 2003 database it worked.......

So you saw the above nightmare. This is what works:

Private Sub ServerDBConnection()
    ' Procedure is used to create the connection to the server database
    Dim wsp As Workspace
    Dim sPath As String
    Dim sPW As String
    
    
    sPath = GetSystemData("datafile")
    sPW = ";PWD=" + GetSystemData("password")
    
    Set wsp = DBEngine.Workspaces(0)
    Set ServerDB = wsp.OpenDatabase(sPath, False, False, sPW)
    
    '  display the last process time
    Me.txtLastTime.Value = sysProcessTime(sysProcess, Now(), "sysRetrieve")
End Sub

Open in new window

0
 
aikimarkCommented:
Can you be more specific?
Rather than iterating one recordset and appending its fields to a row in another recordset, you should write an append query to directly transfer the data from one table to another.  You would probably start with the existing (Select) queries and rewrite them as (Insert) append queries.

You can use the IN keyword to point to the \\fr01apthr001\HRData\HRIS\App\pcdata.accdb database.  Alternatively, you could attach the tables in the target/external database.

The point is probably moot, since you seem to have a solution to your problem.
0
 
dminx13Author Commented:
Code was re-written for 2010 by a contractor. Did not work but after puttingback to original 2003 code it worked fine. No assistance needed to correct at this point
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 8
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now