[英]How to populate Access 2010 text boxes on a Form using VBA code
I have an Access 2010 Form that has three Combo boxes for Year
, Policy Number
, and Type_Code
.我有一个 Access 2010 表单,其中包含三个组合框,分别是Year
、 Policy Number
和Type_Code
。 Next to those I have a policy find button, below all of these I have text boxes that will hold policy data.在那些我有一个策略查找按钮的旁边,在所有这些按钮下面我有保存策略数据的文本框。 When a user select the year, policy number and type code from the combo boxes, they then click on the policy find button.当用户从组合框中选择年份、保单编号和类型代码时,他们然后单击保单查找按钮。 The policy find button then runs the below code that find the policy data for the policy that was selected in the combo boxes.然后,策略查找按钮运行以下代码,为在组合框中选择的策略查找策略数据。
Private Sub PolSearch_Click()
On Error GoTo Err_PolSearch_Click
Dim SQL As String
Dim BD, PRC, PP, I, RFD, TAD, CPYMT, LC, PN, TYP, RY As String
PN = Me!cboPN.Value
TYP = Me!cboTYP.Value
RY = Me!cboRY.Value
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT DISTINCT GA_Loss_Credit.Billing_Date AS BD,
GA_Loss_Credit.Practice_Code as PRC, GA_Loss_Credit.producer_premium as PP,
GA_Loss_Credit.Interest as I, " & _
" GA_Loss_Credit.Refund_Amount as RFD, GA_Loss_Credit.Balance_Due as TD,
GA_Loss_Credit.Payment_Amount AS CPYMT, " & _
" GA_Loss_Credit.Loss_Credit as LC " & _
" FROM [GA_Loss_Credit] " & _
(" WHERE (([GA_Loss_Credit].REINSURANCE_YEAR) ='" & [RY] & "')
AND (([GA_Loss_Credit].POLICY_NUMBER) ='" & [PN] & "')
AND (([GA_Loss_Credit].TYPE_CODE)='" & [TYP] & "')") & _
(" ORDER BY GA_Loss_Credit.Billing_Date ;"))
While Not rs.EOF
BDATE.SetFocus
If IsNull(BDATE.Value) Then
BDATE.Value = rs![BD]
ElseIf IsNull(BDate_2.Value) Then
BDate_2.Value = rs![BD]
ElseIf IsNull(BDate_3.Value) Then
BDate_3.Value = rs![BD]
ElseIf IsNull(BDate_4.Value) Then
BDate_4.Value = rs![BD]
ElseIf IsNull(BDate_5.Value) Then
BDate_5.Value = rs![BD]
ElseIf IsNull(BDate_6.Value) Then
BDate_6.Value = rs![BD]
ElseIf IsNull(BDate_7.Value) Then
BDate_7.Value = rs![BD]
ElseIf IsNull(BDate_8.Value) Then
BDate_8.Value = rs![BD]
ElseIf IsNull(BDate_9.Value) Then
BDate_9.Value = rs![BD]
ElseIf IsNull(BDate_10.Value) Then
BDate_10.Value = rs![BD]
End If
PCode_1.SetFocus
If IsNull(PCode_1.Value) Then
PCode_1.Value = rs![PRC]
ElseIf IsNull(PCode_2.Value) Then
PCode_2.Value = rs![PRC]
ElseIf IsNull(PCode_3.Value) Then
PCode_3.Value = rs![PRC]
ElseIf IsNull(PCode_4.Value) Then
PCode_4.Value = rs![PRC]
ElseIf IsNull(PCode_5.Value) Then
PCode_5.Value = rs![PRC]
ElseIf IsNull(PCode_6.Value) Then
PCode_6.Value = rs![PRC]
ElseIf IsNull(PCode_7.Value) Then
PCode_7.Value = rs![PRC]
ElseIf IsNull(PCode_8.Value) Then
PCode_8.Value = rs![PRC]
ElseIf IsNull(PCode_9.Value) Then
PCode_9.Value = rs![PRC]
ElseIf IsNull(PCode_10.Value) Then
PCode_10.Value = rs![PRC]
End If
rs.MoveNext
Wend
rs.Close
Exit_PolSearch_Click:
Exit Sub
Err_PolSearch_Click:
MsgBox Err.Description
Resume Exit_PolSearch_Click
End Sub
The query that is being run can bring back up to 10 records for Billing Dates
and Practice Types
.正在运行的查询最多可以为Billing Dates
和Practice Types
带回 10 条记录。
The above codes works but my question is;上面的代码有效,但我的问题是; is there a better way of doing this instead of having to write ten different IF Then Else statement.有没有更好的方法来做到这一点,而不必编写十个不同的 IF Then Else 语句。
Any help/suggestion would be greatly appreciated任何帮助/建议将不胜感激
There is a better way to do this.有一种更好的方法可以做到这一点。 What you want to do is use a counter and then call the appropriate control.你想要做的是使用一个计数器,然后调用适当的控件。
First, rename the first box to "BDATE_1" instead of just "BDATE"首先,将第一个框重命名为“BDATE_1”而不是“BDATE”
Dim controlCounter As Integer
.... other code
controlCounter = 1
While (Not rs.EOF) And controlCounter <= 10
Me.Controls("BDATE_" & controlCounter).Value = rs![BD]
Me.Controls("PCode_" & controlCounter).Value = rs![PRC]
controlCounter = controlCounter + 1
rs.MoveNext
Wend
....rest of code
Note that I put in an additional check to make sure the counter doesn't get too big to exceed the comboboxes that you have.请注意,我进行了额外的检查以确保计数器不会变得太大而无法超过您拥有的组合框。
This code will at least populate your boxes.此代码至少会填充您的框。 There may be a better way to design the whole form overall such as by using a listbox instead of a bunch of textboxes to hold the info and setting the listbox.recordsource property to the query you used for the recordset, but I don't entirely understand, and didn't take time to figure out, what exactly the point of your form is.可能有更好的方法来设计整个表单,例如使用列表框而不是一堆文本框来保存信息并将 listbox.recordsource 属性设置为您用于记录集的查询,但我不完全理解,并没有花时间弄清楚,你的表格到底有什么意义。
controlCounter = 1 While (Not rs.EOF) And controlCounter <= 108 controlCounter = 1 While (Not rs.EOF) And controlCounter <= 108
Me.Controls("BDATE_" & controlCounter).Value = rs![BD] Me.Controls("BDATE_" & controlCounter).Value = rs![BD]
Me.Controls("PCode_" & controlCounter).Value = rs![PRC] Me.Controls("PCode_" & controlCounter).Value = rs![PRC]
Me.Controls("PPrem_" & controlCounter).Value = rs![PP] Me.Controls("PPrem_" & controlCounter).Value = rs![PP]
Me.Controls("Inter_" & controlCounter).Value = rs![I] Me.Controls("Inter_" & controlCounter).Value = rs![I]
Me.Controls("RAmt_" & controlCounter).Value = rs![RFD] Me.Controls("RAmt_" & controlCounter).Value = rs![RFD]
Me.Controls("TDue_" & controlCounter).Value = rs![TD] Me.Controls("TDue_" & controlCounter).Value = rs![TD]
Me.Controls("PPay_" & controlCounter).Value = rs![CPYMT] Me.Controls("PPay_" & controlCounter).Value = rs![CPYMT]
Me.Controls("LCR_" & controlCounter).Value = rs![LC] Me.Controls("LCR_" & controlCounter).Value = rs![LC]
controlCounter = controlCounter + 1控制计数器 = 控制计数器 + 1
rs.MoveNext
Wend
rs.Close
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.