访问变量未定义错误

时间:2016-03-03 16:49:59

标签: vba ms-access access-vba ms-access-2007

我在Access表单中有以下VB代码。我已将字段Market添加到我的查询中,字符串太长。所以我不得不把它分成两个陈述。它似乎工作。我的问题在于:

Market = rs("Market")

我得到变量未定义错误。这是在我的查询中。它已添加到生成表单的查询中...我不知道从哪里开始查找...非常感谢任何帮助。

Dim db As DAO.Database, rs As DAO.Recordset
Dim strSQL As String

Set db = CurrentDb()

strSQL = "SELECT TOP 1 CPOrders.Cust, Customer.NAME, CPOrders.CP_Ref, CPOrders.Slsman, CPOrders.Assistant,CPOrders.Date_opn, CPOrders.CPSmall, CPOrders.InvIssu, CPOrders.InvNo, CPOrders.InvDate, CPOrders.DueDate, CPOrders.ETADate, CPOrders.Closed, CPOrders.Cust, CPOrders.Name, CPOrders.BuyerRef, CPOrders.ToCity, CPOrders.ToState, CPOrders.ToCtry, CPOrders.ToPort, CPOrders.Supplier, CPOrders.Origin, CPOrders.Product, CPOrders.GradeType, CPOrders.NoUnits, CPOrders.Pkg, CPOrders.Qty, CPOrders.TotSale, CPOrders.TotCost, CPOrders.GrMargin, CPOrders.[Sale$/Unit], CPOrders.[Cost$/Unit], CPOrders.OceanCost, CPOrders.OceanNotes, CPOrders.BLadingDate, CPOrders.USAPort, CPOrders.FOBCost, CPOrders.FASExportVal, CPOrders.InlandFrt, CPOrders.CommodCode, CPOrders.Notes, CPOrders.ProjCust, CPOrders.ProjValue, CPOrders.ContainerNumber, CPOrders.Vessel, CPOrders.Ord_Terms, CPOrders.Invoice_ID, CPOrders.Movement_Type, CPOrders.Market " & vbCrLf

strSQL = strSQL & "FROM Customer INNER JOIN CPOrders ON Customer.[CUST_#] = CPOrders.Cust ORDER BY CPOrders.Invoice_ID desc;"

Set rs = db.OpenRecordset(strSQL, dbOpenDynaset, dbReadOnly)
rs.MoveFirst





CP_Ref.Value = rs("CP_Ref")
Slsman.Value = rs("Slsman")
Assistant.Value = rs("Assistant")
Date_opn.Value = rs("Date_opn")
CPSmall.Value = rs("CPSmall")
InvIssu.Value = rs("InvIssu")
InvNo.Value = rs("InvNo")
InvDate.Value = rs("InvDate")
DueDate.Value = rs("DueDate")
Cust.Value = rs("Cust")
BuyerRef.Value = rs("BuyerRef")
ToCity.Value = rs("ToCity")
ToState.Value = rs("ToState")
ToCtry.Value = rs("ToCtry")
ToPort.Value = rs("ToPort")
Product.Value = rs("Product")
Supplier.Value = rs("Supplier")
GradeType.Value = rs("GradeType")
Origin.Value = rs("Origin")
NoUnits.Value = rs("NoUnits")
Pkg.Value = rs("Pkg")
Qty.Value = rs("Qty")
TotSale.Value = rs("TotSale")
TotCost.Value = rs("TotCost")
OceanCost.Value = rs("OceanCost")
OceanNotes.Value = rs("OceanNotes")
BLadingDate.Value = rs("BLadingDate")
USAPort.Value = rs("USAPort")
FOBCost.Value = rs("FOBCost")
Notes.Value = rs("Notes")
InlandFrt.Value = rs("InlandFrt")
CommodCode.Value = rs("CommodCode")
Vessel.Value = rs("Vessel")
ContainerNumber.Value = rs("ContainerNumber")
ProjCust.Value = rs("ProjCust")
ProjValue.Value = rs("ProjValue")
Movement_Type.Value = rs("Movement_Type")
Ord_Terms.Value = rs("Ord_Terms")
Market.Value = rs("Market")



rs.Close
db.Close

2 个答案:

答案 0 :(得分:1)

当Access在此行抱怨“变量未定义”时......

Market = rs("Market")

......问题出在行尾的Market; Access认为这是未声明变量的名称。如果问题是rs("Market"),则错误消息将是关于在集合中找不到的项目。

如果您的表单不包含名为 Market 的控件,则可能会遇到您遇到的问题。无论是否为解释,在控件名称前加上Me!,以便Access理解您在表单的默认集合(即Controls集合)中的命名项... ...

Me!Market.Value = rs("Market")

答案 1 :(得分:0)

试试这个:

Dim db As DAO.Database, rs As DAO.Recordset
Dim strSQL As String

Set db = CurrentDb()

strSQL = "SELECT TOP 1 CPOrders.Cust, Customer.NAME, CPOrders.CP_Ref, CPOrders.Slsman, CPOrders.Assistant, CPOrders.Date_opn, " & _
strSQL = strSQL & "CPOrders.CPSmall, CPOrders.InvIssu, CPOrders.InvNo, CPOrders.InvDate, CPOrders.DueDate, " & _
strSQL = strSQL & "CPOrders.ETADate, CPOrders.Closed, CPOrders.Cust, CPOrders.Name, CPOrders.BuyerRef, CPOrders.ToCity, " & _
strSQL = strSQL & "CPOrders.ToState, CPOrders.ToCtry, CPOrders.ToPort, CPOrders.Supplier, CPOrders.Origin, CPOrders.Product, " & _
strSQL = strSQL & "CPOrders.GradeType, CPOrders.NoUnits, CPOrders.Pkg, CPOrders.Qty, CPOrders.TotSale, CPOrders.TotCost, " & _
strSQL = strSQL & "CPOrders.GrMargin, CPOrders.[Sale$/Unit], CPOrders.[Cost$/Unit], CPOrders.OceanCost, CPOrders.OceanNotes, " & _
strSQL = strSQL & "CPOrders.BLadingDate, CPOrders.USAPort, CPOrders.FOBCost, CPOrders.FASExportVal, CPOrders.InlandFrt, " & _
strSQL = strSQL & "CPOrders.CommodCode, CPOrders.Notes, CPOrders.ProjCust, CPOrders.ProjValue, CPOrders.ContainerNumber, CPOrders.Vessel, " & _
strSQL = strSQL & "CPOrders.Ord_Terms, CPOrders.Invoice_ID, CPOrders.Movement_Type, CPOrders.Market " & _
strSQL = strSQL & "FROM Customer INNER JOIN CPOrders ON Customer.[CUST_#] = CPOrders.Cust ORDER BY CPOrders.Invoice_ID desc;"

Set rs = db.OpenRecordset(strSQL, dbOpenDynaset, dbReadOnly)
rs.MoveFirst

CP_Ref.Value = rs("CP_Ref")
Slsman.Value = rs("Slsman")
Assistant.Value = rs("Assistant")
Date_opn.Value = rs("Date_opn")
CPSmall.Value = rs("CPSmall")
InvIssu.Value = rs("InvIssu")
InvNo.Value = rs("InvNo")
InvDate.Value = rs("InvDate")
DueDate.Value = rs("DueDate")
Cust.Value = rs("Cust")
BuyerRef.Value = rs("BuyerRef")
ToCity.Value = rs("ToCity")
ToState.Value = rs("ToState")
ToCtry.Value = rs("ToCtry")
ToPort.Value = rs("ToPort")
Product.Value = rs("Product")
Supplier.Value = rs("Supplier")
GradeType.Value = rs("GradeType")
Origin.Value = rs("Origin")
NoUnits.Value = rs("NoUnits")
Pkg.Value = rs("Pkg")
Qty.Value = rs("Qty")
TotSale.Value = rs("TotSale")
TotCost.Value = rs("TotCost")
OceanCost.Value = rs("OceanCost")
OceanNotes.Value = rs("OceanNotes")
BLadingDate.Value = rs("BLadingDate")
USAPort.Value = rs("USAPort")
FOBCost.Value = rs("FOBCost")
Notes.Value = rs("Notes")
InlandFrt.Value = rs("InlandFrt")
CommodCode.Value = rs("CommodCode")
Vessel.Value = rs("Vessel")
ContainerNumber.Value = rs("ContainerNumber")
ProjCust.Value = rs("ProjCust")
ProjValue.Value = rs("ProjValue")
Movement_Type.Value = rs("Movement_Type")
Ord_Terms.Value = rs("Ord_Terms")
Market.Value = rs("Market")

rs.Close
db.Close