使用代码给ACCESS窗体中32个文本框赋不同值的实例

 借助ACCESS制作抗原试剂条展示报告,以便管理复工员工抗原自测

最近复工的工厂,每天要提供复工人员抗原试剂检测结果的照片给工业区管委会,为了集中展示抗原试剂条,笔者尝试利用ACCESS的窗体,制作了抗原试剂结果存放报告页面。如下图。
《使用代码给ACCESS窗体中32个文本框赋不同值的实例》
窗体上部受测人员标签右侧的文本框内,显示本页面摆放试剂条的人员名单。窗体下部32个文本框,也显示本页人员名单,用于指导人员试剂条摆放位置。上下两部分人员姓名的顺序保持相同。为此,产生了一个问题,如何通过代码给多个文本框赋特定的值。
笔者通过实验,找到了一种方法,供有兴趣的朋友参考。

Private Sub Form_Load()

Dim arry() As String
Dim i As Integer
Dim j As Integer
Dim NameList As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim strTemp As String
Dim ctl As Control
Dim intTxtNum As Integer
Dim intDeptNum As Integer
Dim intTotal As Integer
Dim intPageTotal As Integer
 Me.LablPrinted.Visible = False
 Me.txtDayDisplay = Date + daychoose
 Me.OptTomorrow.Visible = True
' Me.OptTomorrow = False
 Me.明日.Visible = True
 Me.OptLianyin.Visible = True
 Me.OptLianyin = False
 Me.连续打印.Visible = True

'切换页面显示前,首先清除32个名字文本框内的内容
For Each ctl In Me.Controls
    If ctl.ControlType = 109 Then
        For i = 1 To 32
            If ctl.TabIndex = i Then ctl = ""
        Next
    End If
Next

 '给窗体内相关文本框赋值名字或名字清单
 Me.txtPageNum = 1
 NameList = ""
 '使用DLookup函数,此方法对员工序号的编写有特定规则要求:最后一位是部门ID号,前面位是序号
' intDeptNum = Int(Val(Me.txtPageNum))
' j = DCount("姓名", "驻厂人员名单", "部门ID=" & intDeptNum & "And [在公司]=" & 1)
' ReDim arry(j) As String
' For i = 0 To (j - 1)
' If IsNull(DLookup("[姓名]", "驻厂人员名单", "[序号]=" & ((i + 1) * 10 + intDeptNum) & "And [在公司]=" & 1)) Then
' arry(i) = "未驻厂"
' Else
' arry(i) = DLookup("[姓名]", "驻厂人员名单", "[序号]=" & ((i + 1) * 10 + intDeptNum) & "And [在公司]=" & 1)
' NameList = NameList & arry(i) & ","
' intTxtNum = intTxtNum + 1
' For Each ctl In Me.Controls
' If ctl.ControlType = 109 Then
' If ctl.TabIndex = intTxtNum Then
' ctl = arry(i)
' Exit For
' End If
' End If
' Next
' End If
' Next
'使用记录集操作
strTemp = "驻厂人员名单查询"
rs.Open strTemp, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
j = rs.RecordCount
rs.MoveFirst
For i = 1 To j
    If rs("部门ID") = Me.txtPageNum And rs("在公司") = 1 Then
        NameList = NameList & rs("姓名") & ","
        intTxtNum = intTxtNum + 1
        For Each ctl In Me.Controls
            If ctl.ControlType = 109 Then
                If ctl.TabIndex = intTxtNum Then
                    ctl = rs("姓名")
                    Exit For
                End If
            End If
        Next
    End If
    rs.MoveNext
Next
rs.Close
Set rs = Nothing

If Len(NameList) > 0 Then
    Me.txtRenyuan = Left(NameList, Len(NameList) - 1)
End If

'显示页面人数及总人数
intDeptNum = Int(Val(Me.txtPageNum))
intTotal = DCount("姓名", "驻厂人员名单", "在公司=" & 1)
intPageTotal = DCount("姓名", "驻厂人员名单", "部门ID=" & intDeptNum & "And 在公司=" & 1)
Me.Total = intPageTotal & "/" & intTotal

Me.txtPageNum.SetFocus

Set ctl = Nothing
End Sub

代码实现的结果如下图。
《使用代码给ACCESS窗体中32个文本框赋不同值的实例》

    原文作者:Foresttu
    原文地址: https://blog.csdn.net/Foresttu/article/details/124973543
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞