存储行源查询生成的列表框值

时间:2016-06-13 18:08:50

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

我正在尝试存储通过行源查询生成的列表框(Actual_Polygon_Area)的值。每次运行脚本时,我的消息框都会告诉我列表框的值为null,我怀疑它是由行源引起的。可疑列表框名为Actual_Polygon_Area,我尝试存储的字段是表FS_Actual_Polygon中的Polygon_Area。

Private Sub Actual_Polygon_Save_Click()

If IsNull(Actual_Polygon_Year) Then
    MsgBox "Please enter a year in which this polygon received treatment"
    Actual_Polygon_Year.SetFocus
ElseIf IsNull(Actual_Polygon_Season) Then
    MsgBox "Please enter the season in which this polygon received treatment"
    Actual_Polygon_Season.SetFocus
ElseIf IsNull(Actual_Polygon_Treatment) Then
    MsgBox "Please select the treatment type that was completed in this polygon."
    Actual_Polygon_Treatment.SetFocus
ElseIf IsNull(Actual_Polygon_Notes) Then
    MsgBox "Please write a short summary regarding treatment goals and objectives."
    Actual_Polygon_Notes.SetFocus
ElseIf IsNull(Actual_Polygon_Area) Then
    MsgBox "Polygon Area is null, please enter a value"
    Actual_Polygon_Area.SetFocus

Else

    Dim MyConnection As ADODB.Connection
    Set MyConnection = CurrentProject.Connection

    Dim rsFS_Actual_Polygon As ADODB.Recordset
    Set rsFS_Actual_Polygon = New ADODB.Recordset


    If IsNull(PolygonNo) Then
        'add
        rsFS_Actual_Polygon.Open "select * from FS_Actual_Polygon where PolygonNo= " & Actual_Polygon_ID & " and Treatment_Season= '" & Actual_Polygon_Season & "' and Treatment_Type= '" & Actual_Polygon_Treatment & "' and Project_Name = '" & Actual_Polygon_Project_Name & "' and Polygon_Area = " & Actual_Polygon_Area.Value & " and Treatment_Year = " & Actual_Polygon_Year, _
            MyConnection, adOpenDynamic, adLockOptimistic

    If Not rsFS_Actual_Polygon.EOF Then
        MsgBox "The combination of Polygon ID, treatment-year, treatment-season, and treatment type already exist.  Please check the combination."
        Actual_Polygon_Year.SetFocus
    Else
        With rsFS_Actual_Polygon
        .AddNew
            ![PolygonID] = Actual_Polygon_ID
            ![Project_Name] = Actual_Polygon_Project_Name
            ![Polygon_Area] = Actual_Polygon_Area.Value
            ![Treatment_Year] = Actual_Polygon_Year
            ![Treatment_Season] = Actual_Polygon_Season
            ![Treatment_Type] = Actual_Polygon_Treatment
            ![Polygon_Notes] = Actual_Polygon_Notes
        .Update
        End With

        Actual_Polygon_Record.Requery
        Actual_Polygon_New_Click
    End If

如果有必要,我可以发布其余的代码,我只是不想发布一大块。

1 个答案:

答案 0 :(得分:1)

您可以使用.ItemsSelected.ItemData方法查找列表框中所选行的值并将其传递给您的表格。

这是列表框控件的一个相当典型的用法,并且比处理多选控件要容易一些,但仍然有点麻烦。您需要创建一个For Each...Next循环来收集列表框中所选行的行号并保存到变量中。然后使用该变量作为.ItemData方法的参数,以返回所选行的绑定列值。

这篇MSDN文章应该包含您需要的所有内容。

编辑虽然上述解决方案适用于多选和单选列表框,但单选列表框更方便:

Dim i as Integer
i = Me!Actual_Polygon_Area.ListIndex
If i = -1 Then
  MsgBox "No item has been selected"
  Exit Sub
End If
'Do above prior to `WITH` loop
...'Then in your `WITH` loop:
![Polygon_Area] = Actual_Polygon_Area.ItemData(i)