Runtime error '91': Object variable or with block variable not set

spyla

New Member
Joined
Jan 18, 2011
Messages
16
I have two workbooks,

VBA code in book 1

Code:
Private Connect_String As String

Code:
Sub define_connect_string()
Connect_String = "DSN=dev;UID=clgXXX;PWD=XXXXX;DB=DEV"
End Sub

Code:
Function Get_Gas(RecordName As String)
Dim QueryText As String
Dim QueryText1 As String
Dim QueryText2 As String
Dim QueryText3 As String
Dim QueryText4 As String
Dim Conn1 As ADODB.Connection
Dim Cmd1 As ADODB.Command
Dim Rs1 As ADODB.Recordset
 
define_connect_string
 
   Set Conn1 = New ADODB.Connection
   Conn1.ConnectionString = Connect_String
   Conn1.Open
 
    Range("import_gas_range").ClearContents
     QueryText1 = "select "
     QueryText2 = Range("Gas_SQL_Fields_String").Value
     QueryText3 = " from fl_gas where id = "
     QueryText4 = "'" & RecordName & "'"
     QueryText = QueryText1 & QueryText2 & QueryText3 & QueryText4
 
    Cmd1.CommandText = QueryText
  Set Rs1 = Cmd1.Execute()
 
 
    Sheets("Database").Unprotect
        Range("D7").CopyFromRecordset Rs1
 
    Sheets("Database").Protect
      Application.StatusBar = ""
 
Rs1.Close
   Set Rs1 = Nothing
'Return TRUE if a record was returned
If Range("gas_count").Value = 0 Then
    Let Get_Gas = False
    Else: Let Get_Gas = True
End If
End Function


code in book 2

Code:
Sub oraclegetdata()
   Dim Conn1 As ADODB.Connection
   Dim Cmd1 As ADODB.Command
   Dim Rs1 As ADODB.Recordset
   Dim strTmp As String
   Dim sconnect As String
 
     sconnect = "DSN=dev;UID=clgxxx;PWD=XXXXXX;DB=DEV"
 
.
   Set Conn1 = New ADODB.Connection
   Conn1.ConnectionString = sconnect
   Conn1.Open
 
   Set Cmd1 = New ADODB.Command
   Cmd1.ActiveConnection = Conn1
 
        str1 = "SELECT AIR, BENZENE, CLIENT, CO2, CYCLOHEXANE, CYLINDER, DECANES, DODECANES, E_BENZENE, EICOSANES, ETHANE, H2S, HEPTADECANES, HEPTANES, HEXADECANES, HEXANES, HYDROGEN, I_BUTANE, I_PENTANE, ID" & Chr(13) & "" & Chr(10) & "FROM FL_GAS" & Chr(13) & "" & Chr(10) & "WHERE (ID ='"
    str2 = "794"
    Str3 = "')"
    strf = str1 + str2 + Str3
 
 
    Cmd1.CommandText = strf
  Set Rs1 = Cmd1.Execute()
  Range("A5").CopyFromRecordset Rs1
 
1 Rs1.Close
   Set Rs1 = Nothing
End Sub

code 2 runs fine but code 1 fails @

Code:
Cmd1.CommandText = strf

with error
Runtime error '91':

Object variable or with block variable not set

I have made sure that strf and QueryText are exactly same.

any help is appreciated, thanks
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
sorry meant to say

Cmd1.CommandText = QueryText

running Off 2010 Prof Plus 64bit on Win 7 Ent 64 bit
 
Upvote 0
found the error

forgot the lines

Code:
Set Cmd1 = New ADODB.Command
   Cmd1.ActiveConnection = Conn1
after

Code:
Conn1.Open
 
Upvote 0

Forum statistics

Threads
1,224,567
Messages
6,179,571
Members
452,927
Latest member
whitfieldcraig

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top