MS Access,ComboBox值转换为SQL语句

时间:2013-10-28 11:28:21

标签: sql ms-access vb6

我的表单的功能是允许用户为另一个用户分配任务,这很好。用户通过链接到访问数据库的组合框选择他们想要分配任务的人,我现在正在尝试实现一个功能,我可以从组合框中获取所选用户的值(通过匹配ID中的ID)数据库中的ID的combox,然后从该行查找电子邮件,然后我可以将其插入到当用户单击“分配”时弹出的Outlook电子邮件中(Outlook代码工作正常,它只是没有拉动组合框中的值,所以单击它会使outlook新电子邮件向上,但“To”字段为空,我还尝试打印我分配给该字段的变量,以确保它不是Outlook代码的问题并且它仍然没有返回任何值。)

以下是无法使用的代码部分....

Dim OutApp As Object
Dim OutMail As Object
Dim EmailSubject As String
Dim EmailSendTo As String
Dim MailBody As String
Dim db As DAO.Database, rs As DAO.Recordset
Dim s As String
Dim sqlStatement As String

Combo2.SetFocus

Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT tblUsers.Email FROM tblUsers WHERE tblUsers.UserID = 'Combo2.Text ")

Combo2是组合框的名称

3 个答案:

答案 0 :(得分:0)

可能是这样吗?

Set rs = db.OpenRecordset("SELECT tblUsers.Email FROM tblUsers WHERE tblUsers.UserID = " & Combo2.SelectedItem.ToString())

答案 1 :(得分:0)

试试这个:

如果您的UserID为Text,请尝试以下方法:

Set rs = db.OpenRecordset("SELECT tblUsers.Email FROM tblUsers WHERE tblUsers.UserID = '" & Combo2.Text & "'")

但是,如果您的UserID为numeric,请尝试以下方法:

Set rs = db.OpenRecordset("SELECT tblUsers.Email FROM tblUsers WHERE tblUsers.UserID = " & Combo2.Text)

答案 2 :(得分:0)

我不完全理解Access如何发挥作用,所以如果这是错误的我道歉。这就是我在Access中的方式:

Dim OutApp As Object
Dim OutMail As Object
Dim EmailSubject As String
Dim EmailSendTo As String
Dim MailBody As String
Dim db As DAO.Database, rs As DAO.Recordset
Dim EmailTo as String
Dim s As String
Dim sqlStatement As String

Combo2.SetFocus

Set db = CurrentDb
EmailTo = DLookup ("Email", "tblUsers", "tblUsers.UserID = '" & Combo2.Text & "'")

如果您的UserID是INT,您可以将最后一行更改为:

EmailTo = DLookup ("Email", "tblUsers", "tblUsers.UserID = " & Combo2.Text & "")

顺便说一句,你说Edper的解决方案“没有拿起任何东西”。你知道“rs”在这种情况下不具有实际价值,对吧?但是,rs(“电子邮件”)将添加:

EmailSendTo = rs("Email")

在他的SQL字符串之后,可能会给你你需要的东西。