如何在Access中将字符串连接到SQL查询?

时间:2014-01-15 15:12:31

标签: sql ms-access ms-access-2010

基本上我试图使用团队成员名称自动填充下拉框,这样在将来随着团队成员被删除并添加下拉列表将自动调整。这是一个简单的部分,困难的部分是我还需要“全部”显示在下拉框中。虽然我似乎无法找到任何方法来实现这一点,所以我正在寻找任何想法,或者有人告诉我这是不可能的。

此时我只需将dropdown rowsource设置为'Table / Query',查询为:

 SELECT [Team Table].[Team_Member_ID], [Team Table].[USER_ID] 
    FROM [Team Table] 
    WHERE [Active] = True 
    ORDER BY [USER_ID] ;

我已经在UNION SELECTUNION ALL SELECT中添加了多次尝试,但我看不到要运行,SQL不是我最强的套装,所以这可能只是我。

3 个答案:

答案 0 :(得分:4)

使用联合

SELECT 0     AS [Team_Member_ID], 'All' AS [USER_ID] 
UNION 
SELECT [team table].[team_member_id], [team table].[user_id] 
FROM   [team table] 
WHERE  [active] = true 
ORDER  BY [user_id];  

答案 1 :(得分:2)

试试这个。这将在顶部显示ALL并按USER_ID

对其余部分进行排序
SELECT * FROM 
(
     SELECT -1 as [Team_Member_ID], 'ALL' as [USER_ID]
     UNION   
     SELECT [Team Table].[Team_Member_ID], [Team Table].[USER_ID] FROM [Team Table] WHERE [Active] = True 
) as a
ORDER BY CASE WHEN  [USER_ID] = 'ALL' THEN 0 ELSE 1 END, [USER_ID]

答案 2 :(得分:0)

我最终不得不创建这个功能来做我需要的事情。

 
Sub ValueList(ByRef rs As Recordset, _
          strReturnColumn As String, _
          strCtrlToChange As String, _
          strDefaultValueIn As String, _
          frm As Form)


    Dim strRowSource    As String
    Dim strDefaultValue As String

    strDefaultValue = strDefaultValueIn
    strRowSource = ""
    rs.MoveFirst

    'If there is a Default Value add it to the RowSource
    If strDefaultValue <> "" Then
        strRowSource = strRowSource & strDefaultValue & ";"
    End If

    'Add all results onto the RowSource
    Do While Not rs.EOF
        If CStr(rs.Fields(strReturnColumn) & "") <> "" Then
            strRowSource = strRowSource & CStr(rs.Fields(strReturnColumn) & "") & ";"
        End If
        rs.MoveNext
    Loop

    'Set Rowsource and default value
    frm.Controls(strCtrlToChange).RowSource = strRowSource
    frm.Controls(strCtrlToChange).DefaultValue = "'" & strDefaultValue & "'"

End Sub