-1

在 Access 2010 中,我需要能够单击一个命令按钮,该按钮将运行一个返回小的两个字段记录集的查询。然后将该记录集中的第二个字段放入字符串变量中。

这个字符串变量是网络上一个word文档的链接。然后代码的第二部分将打开word文档。任何帮助是极大的赞赏。

我收到错误:“对象变量或未设置块变量”

我的代码如下所示:

    `Option Compare Database

Private 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.PartNum, 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


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
4

2 回答 2

0

你没有发布太多信息,但我收集到你正在寻找的是Recordset

Dim db As Database
Dim rs As Recordset
Dim s As String
Dim myString As String

s = "SELECT * FROM myTable1;" 'Replace with the SQL you need
Set db = CurrentDb
Set rs = db.OpenRecordset(s)

myString = rs.Fields("myFieldName1")  'Replace myFieldName with the appropriate field name

rs.Close
于 2013-07-10T18:44:15.640 回答
0

在剖析你的代码之后..这个:

"..WHERE (((tblParts.PartNum)=[Forms]![frmSpecSheet]![cboPartNum]));"

将单词Forms!etc嵌入到您的 sql 语句中,它不会将组合框值插入到语句中。

您需要从字符串中取出表单引用:

    Dim db As Database
    Dim rs As Recordset
    Dim s As String
    Dim specSheet As String
    s = "SELECT tblParts.PartNum, tblParts.CE_SpecSheet FROM tblParts WHERE " _
        & "tblParts.PartNum=" & [Forms]![frmSpecSheet]![cboPartNum]
    'Chooses the Correct Spec Sheet
    Set db = CurrentDb
    Set rs = db.OpenRecordset(s)
    specSheet = rs.Fields("CE_SpecSheet")
    'Chooses the Spec Sheet Field
    rs.Close
    Dim oApp As Object
    Set oApp = CreateObject("Word.Application")
    oApp.Visible = True
    With oApp
        .Documents.Open ("specSheet")
    End With

如果组合框的值为文本,那么您还需要用撇号将其括起来。

您也不需要 SELECT 字段tblParts.PartNum,您可以在 WHERE 子句中引用它。

于 2013-07-10T23:05:31.913 回答