UbiqutyDegins
asked on
Access 2010 VBA automation error
Hello Experts,
I am new to programming and especially new to VBA. So any help would be greatly appreciated.
In Access 2010 I need to be able to click a command button that will run a query that returns a small one field recordset. (This feild is a Hyperlink.) Then put the value of the field in that recordset into a string variable.
This string variable is a link to a word document on the network. the second part of the code will then open the word document.
I am getting the error: "Object variable or With block variable not set"
I am unsure which line is giving me trouble however I think it is erroring out before the line"
My code looks like this:
I am new to programming and especially new to VBA. So any help would be greatly appreciated.
In Access 2010 I need to be able to click a command button that will run a query that returns a small one field recordset. (This feild is a Hyperlink.) Then put the value of the field in that recordset into a string variable.
This string variable is a link to a word document on the network. the second part of the code will then open the word document.
I am getting the error: "Object variable or With block variable not set"
I am unsure which line is giving me trouble however I think it is erroring out before the line"
MsgBox "Spec sheet name: " & specSheet
because this MsgBox never shows up.My code looks like this:
Option Compare Database
Sub cmdCESpec_Click()
On Error GoTo Err_cmdCESpec_Click
Dim db As Database
Dim rs As DAO.Recordset
Dim s As String
Dim specSheet As String
s = "SELECT p.CE_SpecSheet FROM tblParts p WHERE p.PartNum = " & [Forms]![frmSpecSheet]![cboPartNum] 'Chooses the correct Spec Sheet.
Set rs = db.OpenRecordset(s)
specSheet = rs.Fields("CE_SpecSheet") 'Chooses the Spec Sheet Field
MsgBox "Spec sheet name: " & specSheet
rs.Close
Dim oApp As Object
Set oApp = CreateObject("Word.Application")
oApp.Visible = True
With oApp
.Documents.Open (specSheet)
End With
Exit_cmdCESpec_Click:
Exit Sub
Err_cmdCESpec_Click:
MsgBox Err.Description
Resume Exit_cmdCESpec_Click
End Sub
Also, to pinpoint which line is failing, and the error message, temporarily comment out the On Error line. If you get an error, click "Debug" and let us know which line appears highlighted in the debugger.
Try this:
Try this:
Option Compare Database
Sub cmdCESpec_Click()
'On Error GoTo Err_cmdCESpec_Click
Dim db As Database
Dim rs As DAO.Recordset
Dim s As String
Dim specSheet As String
s = "SELECT p.CE_SpecSheet FROM tblParts p WHERE p.PartNum = '" & [Forms]![frmSpecSheet]![cboPartNum] & "'" 'Chooses the correct Spec Sheet.
Set rs = db.OpenRecordset(s)
specSheet = rs.Fields("CE_SpecSheet") 'Chooses the Spec Sheet Field
MsgBox "Spec sheet name: " & specSheet
rs.Close
Dim oApp As Object
Set oApp = CreateObject("Word.Application")
oApp.Visible = True
With oApp
.Documents.Open (specSheet)
End With
Exit_cmdCESpec_Click:
Exit Sub
Err_cmdCESpec_Click:
MsgBox Err.Description
Resume Exit_cmdCESpec_Click
End Sub
ASKER
Yes, PartNum is text. I have made the changes you suggested and there appears to be no change in the programs responce.
Option Compare Database
Sub cmdCESpec_Click()
On Error GoTo Err_cmdCESpec_Click
Dim db As Database
Dim rs As DAO.Recordset
Dim s As String
Dim specSheet As String
s = "SELECT p.CE_SpecSheet FROM tblParts p WHERE p.PartNum = '" & [Forms]![frmSpecSheet]![cboPartNum] & "'" 'Chooses the correct Spec Sheet.
Set rs = db.OpenRecordset(s)
specSheet = rs.Fields("CE_SpecSheet") 'Chooses the Spec Sheet Field
MsgBox "Spec sheet name: " & specSheet
rs.Close
Dim oApp As Object
Set oApp = CreateObject("Word.Application")
oApp.Visible = True
With oApp
.Documents.Open (specSheet)
End With
Exit_cmdCESpec_Click:
Exit Sub
Err_cmdCESpec_Click:
MsgBox Err.Description
Resume Exit_cmdCESpec_Click
End Sub
Okay - take a look at my last comment and try the code exactly as I have posted it. Note the tick mark before the On Error:
Now when you get your error, click Debug... and let me know which line is highlighted.
'On Error GoTo Err_cmdCESpec_Click
Now when you get your error, click Debug... and let me know which line is highlighted.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
The code is erroring out on:
Set rs = db.OpenRecordset(s)
Try this:
Option Compare Database
Sub cmdCESpec_Click()
On Error GoTo Err_cmdCESpec_Click
Dim db As Database
Dim rs As DAO.Recordset
Dim s As String
Dim specSheet As String
Set db = CurrentDB '<---- add this line here
s = "SELECT p.CE_SpecSheet FROM tblParts p WHERE p.PartNum = '" & [Forms]![frmSpecSheet]![cboPartNum] & "'" 'Chooses the correct Spec Sheet.
Set rs = db.OpenRecordset(s)
specSheet = rs.Fields("CE_SpecSheet") 'Chooses the Spec Sheet Field
MsgBox "Spec sheet name: " & specSheet
rs.Close
Dim oApp As Object
Set oApp = CreateObject("Word.Application")
oApp.Visible = True
With oApp
.Documents.Open (specSheet)
End With
Exit_cmdCESpec_Click:
Exit Sub
Err_cmdCESpec_Click:
MsgBox Err.Description
Resume Exit_cmdCESpec_Click
End Sub
ASKER
Good News! That solved this error.
The bad news, now I have a new one, lol.
I'll post a new question.
The bad news, now I have a new one, lol.
I'll post a new question.
ASKER
Thanks for all your help!
You're welcome! Keep that trick of commenting out the ON Error line in mind. It always helps us resolve these questions more quickly if you can let us know the line of code that is raising an error.
It should be:
Open in new window
(Note the embedded single quotes)