我对VBA / SQL很陌生,我试图执行条件内连接。
我有两个共同列的表(" CRM"" CodeCRM")我想从table2获取电子邮件地址(" Desks" )当table1(" Flux")触发某些内容(CodeBlocage = 101)时,将其添加到自动Outlook电子邮件中。
Dim StrDestinataire As String
Select Case Strtable
Case "Flux", "GAFIJOUR"
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim Y As String
Dim sSql As String
Set cn = CurrentProject.Connection
sSql = "Select AddMailCRM from Desks Inner Join Flux on Desks.CODECRM = Flux.CRM WHERE Flux.CODEBLOCAGE = '101'"
Set rs = cn.Execute(sSql)
If Not rs.BOF And Not rs.EOF Then
Y = rs.Fields("AddMailCRM").Value
End If
StrDestinataire = Y
cn.Close
除了应该为电子邮件地址返回多个值之外,一切都很有效。任何线索?
谢谢
答案 0 :(得分:1)
当读出记录集时,你必须逐行遍历记录。使用当前代码,您只需查看第一行。
尝试这样的事情:
Dim results as New List(Of String)
While Not rs.EOF
results.Add(rs.Fields("AddMailCRM").Value)
rs.MoveNext()
End While
results = results.Distinct().ToList
结果变量将是唯一值列表
答案 1 :(得分:1)
解决。
Dim StrDestinataire As String
Select Case Strtable
Case "Flux", "GAFIJOUR"
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim Y As String
Dim sSql As String
Set cn = CurrentProject.Connection
sSql = "Select DISTINCT AddMailCRM from Desks Inner Join Flux on Desks.CODECRM = Flux.CRM WHERE Flux.CODEBLOCAGE = '101'"
Set rs = cn.Execute(sSql)
If Not rs.BOF And Not rs.EOF Then
Y = rs.GetString
End If
StrDestinataire = Y
cn.Close