0

我正在使用列表框,当您单击一行时,它将填充工作表上的字段。因此,如果我单击工单 18,它将从表格​​中获取部门、优先级、受让人等并填写工单。当我点击该行时,我会弹出一个带有票号的消息框,只需编写:

MsgBox Me.Work_Order_List.Value

然后当我尝试

ticNum = Me.Work_Order_List.Value
strSQL = "SELECT Description_Of_Problem FROM Work_Orders WHERE " & ticNum & " = Ticket_Number;"
Debug.Print strSQL
Me.Notes = strSQL

我的注释框将打印 SQL 语句但从不运行。我在这里做错了什么?我将需要填充许多字段,所以还有更快的方法吗?

谢谢

4

3 回答 3

1
Me.Notes = DLookup("Description_Of_Problem", "Work_Orders", "Ticket_Number=" & ticNum)

That was exactly what I was looking for. I was able to delete the rest of my code and use those for each field. Thanks HansUp!

于 2013-02-08T22:54:03.463 回答
0

您没有运行查询。如果您希望数据填写到字段中,您应该将表单的记录源设置为 sql。

 Me.RecordSource = "SELECT Description_Of_Problem, Other, Etc FROM Work_Orders"

上述方法仅在您将控件绑定到字段时才有效,但这是填充大量控件的最简单方法。

最好的开始方式是使用表单向导基于 Work_Orders 创建一个表单。然后,您可以通过创建一个组合框来选择表单上的记录,从而完全避免任何编码。然后,用户可以选择一张票并跳转到该记录。也有一个向导。


Dim rs As DAO.Recordset

ticNum = Me.Work_Order_List.Value
strSQL = "SELECT Description_Of_Problem FROM Work_Orders WHERE Ticket_Number =" _
       & ticNum 
Debug.Print strSQL
Set rs = CurrentDB.OpenRecordset strSQL
Me.Notes = rs!Description_Of_Problem

绑定表单

基于表或查询启动表单向导

Form wizard

按照步骤完成设计视图中的表单

the form in design view

有许多方法可以浏览记录,您似乎喜欢列表框。添加一个列表框,确保选择了该向导。

select listbox

选择在当前表单上查找记录

find record on form

并选择要出现在列表框中的项目

select listbox items

选择一个项目以在表单上找到它。

select item to find record

This is what makes it a bound form, the record source, which can be a table name, a query name or an SQL statement. Note that the control to be filled in automatically are bound to field names, but the listbox to find records is marked 'unbound'.

bound form

于 2013-02-08T22:21:45.773 回答
0

After having to make multiple edits, I found that DLookup was not as convenient with multiple fields and an unbound form. So in the end I have decided:

Dim myR As Recordset

Set myR = CurrentDb.OpenRecordset("Work_Orders", dbOpenDynaset)
myR.FindFirst ("[Ticket_Number] = " & Me.Work_Order_List.Value & "")

Me.Update_Status = myR![Current_Status]
Me.Downtime_Code = myR![Downtime_Code]
Me.Date_For_Completion = myR![Date_For_Completion]
Me.Notes = myR![Notes_From_Assignee]
Me.Description_Box = myR![Description_Of_Problem]
Set myR = Nothing

And I use myR.FindFirst to grab the row I'm looking for. Since my ticket system is still in the testing phase I only have ~100 records, but I'm hoping it will be able to search quickly when it reaches 10000+ with .FindFirst.

于 2013-02-12T23:37:36.810 回答