MS Access VBA IF()

丹尼尔

我正在尝试为MS Access VBA编写if循环

在php中,我会这样写:

if($query=="0"){
  run query 1
} else {
  run query 2
}

我确实需要调用查询并引用多个表(即“查询1”将从组合框中提取数据,而“查询2”将运行另一个查询)

------------------------------------编辑:2014年2月6日13:34 AEST- -------------------------

Dim variabl1 As String
variabl1 = Me.cmbItemDetails.Column(1)
Dim variabl2 As String
variabl2 = "" & Forms!frmRaiseOrder!cmbDebtorCode & ""
'you can use variable as a parameter in SQL, but only if you hardcode your SQL     statement as a string... See directly below

Dim SQL As String
SQL_count = "SELECT COUNT(CustItemPrice) FROM tblSpecialPricing WHERE ItemListID = '" &     variabl1 & "' AND CustListID = '" & variabl2 & "' "
SQL_select = "SELECT CustItemPrice FROM tblSpecialPricing WHERE ItemListID = '" &     variabl1 & "' AND CustListID = '" & variabl2 & "' "


Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset(SQL_count)
If rs.RecordCount = "0" Then
   Me.txtStreetPrice.Value = Me.cmbItemDetails.Column(3)
Else
   DoCmd.OpenQuery "qrySelectCustomerName"
End If

感谢@MurDeR为上述声明提供的帮助






-----------------------------------------------更新于7月9日-----------------------------------

嗨,大家好;
如果尝试在ACCESS VBA中创建IF()语句,请使用以下语法...

Private Sub cmbItemDetails_Change()

Me.txtItemDescription.Value = Me.cmbItemDetails.Column(2)
Me.txtStreetPrice.Value = Me.cmbItemDetails.Column(3)
Me.txtItemName.Value = Me.cmbItemDetails.Column(1)

Dim variabl1 As String
variabl1 = Me.cmbItemDetails.Column(0)
Dim variabl2 As String
variabl2 = "" & Forms!frmRaiseOrder!cmbDebtorCode & ""
'you can use variable as a parameter in SQL, but only if you hardcode your SQL statement as a string... See directly below

Dim SQL As String
SQL_count = "SELECT COUNT(CustItemPrice) FROM tblSpecialPricing WHERE ItemListID = '" & variabl1 & "' AND CustListID = '" & variabl2 & "' "
SQL_select = "SELECT CustItemPrice FROM tblSpecialPricing WHERE ItemListID = '" & variabl1 & "' AND CustListID = '" &     variabl2 & "' "

Dim rs As Recordset
Set rs = Nothing
Set rs = CurrentDb.OpenRecordset(SQL_count)
RecordCount = rs.Fields(0)

If RecordCount = "1" Then
   'SPECIAL PRICE EXISTS - this code will run only if the count query is greater than zero
   MsgBox "Special Price Exists", vbOkay, "Alert"
   '
   '
   Me.txtUnitPrice.Value = Me.cmbItemDetails.Column(3)
   'Me.txtUnitPrice.Value = "" & Forms!frmRaiseOrder!subformCreateOrder!frmSelectCustomPriceinsubform!CustItemPrice & ""
   'Me.txtUnitPrice.Value = Me.subfrmItemPrice.CustItemPrice.Value
Else
   'NO SPECIAL PRICE - this code will run only if the coutn query is zero
   'MsgBox "No Special Pricing for this item", vbOkay, "Alert"
   '
   '
   Me.txtUnitPrice.Value = Me.cmbItemDetails.Column(3)
End If

End Sub




非常感谢@MurDeR的帮助,如果您有任何问题,请告诉我或发贴在这里

马克C.

您可以在VBA中使用记录集。

请参见以下代码。

Dim variabl As String
variabl = Me.ComboBox1.Value
'you can use variabl as a parameter in SQL, but only if you hardcode your SQL statement as a string... See directly below

Dim SQL As String
SQL = "SELECT * FROM TableA WHERE YourColumnName = '" & variabl & "'"


Dim db As DAO.Database
db = CurrentDb
Dim rs As DAO.Recordset
Set rs = db.OpenRecordset("SQL Statement or Query Name")
If rs.RecordCount = 0 Then
DoCmd.OpenQuery "Query1"
Else
DoCmd.OpenQuery "Query2"
End If
rs.Close
Set rs = Nothing

如果您还有其他问题,请告诉我。另请注意,您需要确保输入正确的查询名称。我知道VBA具有可怜的智能感知功能,因此在以字符串形式输入查询名称时,这可能会很棘手。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Int()函数MS Access VBA

来自分类Dev

MS ACCESS vba-错误

来自分类Dev

MS Access VBA 运行查询

来自分类Dev

带VBA的MS Access链接表

来自分类Dev

MS Access中VBA代码的继承

来自分类Dev

多参数IsNull函数MS Access VBA

来自分类Dev

SQL语法问题MS Access VBA

来自分类Dev

MS-Access:使用vba移至NavigationControl

来自分类Dev

MS Access VBA:如何清除图像控件?

来自分类Dev

MS Access VBA:OpenRecordSet返回什么?

来自分类Dev

是/否字段上的MS Access VBA Dlookup

来自分类Dev

MS Access查询未从VBA接收参数

来自分类Dev

MS Access VBA,SQL脚本:In(),<>和AND语法

来自分类Dev

使用VBA显示MS Access报告

来自分类Dev

MS ACCESS VBA按案例订购的时间

来自分类Dev

MS Outlook会干扰Access vba过程

来自分类Dev

MS Access VBA 查询多个表

来自分类Dev

MS Access VBA - 使用日期过滤表单

来自分类Dev

DLookup:MS Access VBA 中的查询与表?

来自分类Dev

MS Access VBA - 同名私人订阅

来自分类Dev

VBA 全屏打开 MS Access 表单

来自分类Dev

MS Access 2013:通过VBA使用MS Word的语法检查

来自分类Dev

如何从MS Access 2003 VBA处理MS Excel分页符

来自分类Dev

使用MS Access在Excel VBA中运行代码

来自分类Dev

MS Access使用VBA从访问文件中删除模块

来自分类Dev

通过VBA在MS Access中编辑当前对象的SQL

来自分类Dev

如何使用VBA创建空白的MS Access数据库?

来自分类Dev

MS Access VBA与AbsolutePosition一起使用

来自分类Dev

MS Access VBA:建议将INT更改为LONG吗?