在access vba中将字段值设为静态

在access vba中将字段值设为静态,vba,Vba,您好,我正在尝试根据Select Case向字段的值添加各种值。我面临的问题是,每当我在select语句中得到不同的大小写时,字段的值(而不是变化的值)会将顶部的值相加 Private Sub ProductID_AfterUpdate() Dim qflPrice As Variant Dim db As DAO.Database Dim rs As DAO.Recordset Dim sqlQry As String Dim instID As Integer instID = Me.F

您好,我正在尝试根据Select Case向字段的值添加各种值。我面临的问题是,每当我在select语句中得到不同的大小写时,字段的值(而不是变化的值)会将顶部的值相加

Private Sub ProductID_AfterUpdate()

Dim qflPrice As Variant
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim sqlQry As String
Dim instID As Integer

instID = Me.Form!ProductID.Value
sqlQry = "SELECT Products.Price FROM Products WHERE Products.ProductID = " & instID & ""
Set db = CurrentDb
Set rs = db.OpenRecordset(sqlQry)
Me.flPrice.Value = rs!Price

End Sub
Private Sub ExtrasID_Change()
Dim extrID As Integer
Dim addNum As Integer
Static floorPrice As Integer
Static sumPrice As Integer

extrID = Me.ExtrasID.Value
floorPrice = Me.flPrice.Value

Select Case extrID

    Case Is = 1
        addNum = 5
        sumPrice = floorPrice + addNum
    Case Is = 2
        addNum = 10
        sumPrice = floorPrice + addNum
    Case Is = 3
        addNum = 15
        sumPrice = floorPrice + addNum
End Select

Me.flPrice.Value = sumPrice

End Sub
替换为:
Me.flPrice.Value=qflPrice

顺便说一句,你应该清理一下你的代码

Private Sub ExtrasID_Change()

Dim extrID As Integer
Static qflPrice As Integer
Static numPrice As Integer

extrID = Me.ExtrasID.Value
numPrice = Me.flPrice.Value

Select Case extrID
    Case Is = 1
        qflPrice = 5
    Case Is = 2
        qflPrice = 10
    Case Is = 3
        qflPrice = 15
End Select

Msgbox qflPrice
Me.flPrice.Value = qflPrice
End Sub

(您甚至可以只执行
Me.flPrice.Value=Me.ExtrasID.Value*5
而不使用select语句,但我猜这只是一个示例)

我通过声明全局变量自己解决了这个问题

Option Compare Database
Public rsqryPrice As Integer
Option Explicit
Private Sub ProductID_AfterUpdate()

    Dim qflPrice As Variant
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim sqlQry As String
    Dim instID As Integer

    instID = Me.Form!ProductID.Value
    sqlQry = "SELECT Products.Price FROM Products WHERE Products.ProductID = " & instID & ""
    Set db = CurrentDb
    Set rs = db.OpenRecordset(sqlQry)
    Me.flPrice.Value = rs!Price
    rsqryPrice = rs!Price

End Sub
Private Sub ExtrasID_Change()
    Dim extrID As Integer
    Static floorPrice As Integer
    Static sumPrice As Integer
    Static numPrice As Integer

    extrID = Me.ExtrasID.Value

    floorPrice = Me.flPrice.Value
    numPrice = rsqryPrice

    Select Case extrID

        Case Is = 1
           Me.flPrice.Value = numPrice + 5

        Case Is = 2
           Me.flPrice.Value = numPrice + 10

        Case Is = 3
           Me.flPrice.Value = numPrice + 15

    End Select
End Sub

这并不能解决我的问题。请看一看我在做什么。只是解决一个问题,停止改变它。澄清一下,这是令人困惑的。看起来像是把sumPrice=floorPrice+addNum改为sumPrice=addNum…你从不同的角度来看,我有一个地板的价格,来自价格表。然后,该价格将保存在订单表中。如果我没有选择任何额外费用,价格将保持不变,额外费用取决于增加的额外费用的种类,它将变为5英镑,而不是10英镑,而不是15英镑。问题是,如果有人为额外费用选择不同的选项,那么在每次更改选项时,它将为底价价值增加相同的价值,我需要保持地板的价值不变,只广告与最终价格相关的数字。
Option Compare Database
Public rsqryPrice As Integer
Option Explicit
Private Sub ProductID_AfterUpdate()

    Dim qflPrice As Variant
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim sqlQry As String
    Dim instID As Integer

    instID = Me.Form!ProductID.Value
    sqlQry = "SELECT Products.Price FROM Products WHERE Products.ProductID = " & instID & ""
    Set db = CurrentDb
    Set rs = db.OpenRecordset(sqlQry)
    Me.flPrice.Value = rs!Price
    rsqryPrice = rs!Price

End Sub
Private Sub ExtrasID_Change()
    Dim extrID As Integer
    Static floorPrice As Integer
    Static sumPrice As Integer
    Static numPrice As Integer

    extrID = Me.ExtrasID.Value

    floorPrice = Me.flPrice.Value
    numPrice = rsqryPrice

    Select Case extrID

        Case Is = 1
           Me.flPrice.Value = numPrice + 5

        Case Is = 2
           Me.flPrice.Value = numPrice + 10

        Case Is = 3
           Me.flPrice.Value = numPrice + 15

    End Select
End Sub