【问题标题】:Updating a record by using a selection box control使用选择框控件更新记录
【发布时间】:2014-07-26 20:03:17
【问题描述】:

我想知道在用户选择记录的选择框控件后立即放入代码字符串的首选函数,以便确认他的选择并将其包含为“真”。简而言之,我有一个表单,用户使用一个选择框来指示要选择哪些记录,然后执行一个命令,其中我有代码应该复制和粘贴他选择的记录。不幸的是,在运行执行复制/粘贴命令时选择的最后一条记录无法识别。我知道我可能需要添加一个功能,例如“转到下一条记录”,但是我不确定这是否是最好的方法,或者程序员是否有更标准的方法来避免丢失选择的最后一条记录。下面是我当前使用的代码,它当前没有获取用户选择的最后一条记录。

Private Sub Comando99_Click()

If CurrentRecord = Recordset.RecordCount And CurrentRecord <> 1 Then
    DoCmd.GoToRecord , "", acFirst
Else
    DoCmd.GoToRecord , "", acNext
End If    


    Dim intAnswer As Integer

On Error GoTo HandleError

    intAnswer = _
        MsgBox("Are you sure you want to add these dependencies to your dependency project tracker?", _
            vbQuestion + vbYesNo, "Add Dependencies")

    If intAnswer = vbYes Then



st_sql = "INSERT INTO [tblDependencies] ( [Description] )SELECT [tblDependencyTypeListing].[Dependency (General)] FROM [tblDependencyTypeListing] WHERE ((([tblDependencyTypeListing].[ToIncludeInProject])=True))"
    Application.DoCmd.RunSQL (st_sql)

st_sql = "UPDATE[tblDependencies],[tblHoldingProjectid]SET[tblDependencies].[ID Project]=[tblholdingprojectid].[ID_Project]where([tbldependencies].[ID Project])=0 and ([tblholdingprojectid].[ID_Project])is not null"
Application.DoCmd.RunSQL (st_sql)



st_sql = "UPDATE[tblDependencies]SET[tblDependencies].[Automatic date of entry]=now() where([tblDependencies].[Automatic date of entry])is null"
Application.DoCmd.RunSQL (st_sql)

st_sql = "UPDATE[tblContacts],[tblDependencies]SET[tblDependencies].[Automatic user entry]=[tblContacts].[Complete name]where([tblContacts].[In use])is not null and([tblDependencies].[Automatic user entry])is null"
Application.DoCmd.RunSQL (st_sql)

st_sql = "UPDATE[tblDependencytypelisting]SET[tblDependencytypelisting].[toincludeinproject]=null"
Application.DoCmd.RunSQL (st_sql)

Me.Refresh




    End If




ExitHere:

    Exit Sub

HandleError:

    MsgBox "Error is " & Err.Description
    Resume ExitHere



End Sub

【问题讨论】:

  • 发布这个问题后,我想我找到了一个可以接受的解决方案。我在例程的开头添加了一个 if 语句,这似乎解决了我的问题,我认为一切正常。但是,我想确认这是否是最好的方法,或者是否有解决此问题的首选方法。

标签: sql ms-access vba ms-access-2010


【解决方案1】:

通过在例程开头输入以下 IF 语句来解决问题,以确保在运行代码之前,ACCESS 移动到下一条记录以确保它确认最后一次修改

If CurrentRecord = Recordset.RecordCount And CurrentRecord <> 1 Then
    DoCmd.GoToRecord , "", acFirst
Else
    DoCmd.GoToRecord , "", acNext
End If    

【讨论】:

    猜你喜欢
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多