非常简单的问题,但现在让我陷入困境。
我想改变性别'这样的领域 如果Gender为0,则为1 如果Gender为1,那么2
这是我的代码:
Private Sub Run_Click()
Dim db As DAO.Database
Dim queryDef As DAO.queryDef
Dim sql As String
Dim fields As Variant
Set db = CurrentDb
sql = ""
fields = Array("Name", "Gender", "Age")
For Each queryDef In db.QueryDefs
If queryDef.Name = "Q_List" Then
db.QueryDefs.Delete "Q_List"
Exit For
End If
Next
sql = sql & "SELECT "
For Each field In fields
If field = "Gender" Then
sql = sql & IIf([Gender] = 0, 1, 2) & " AS Gender, "
Else
sql = sql & "[" & field & "]" & ", "
End If
Next
sql = Left(sql, Len(sql) - 2)
sql = sql & " FROM T_Customer"
Set queryDef = db.CreateQueryDef("Q_List", sql)
DoCmd.TransferText acExportDelim, , "Q_List", "C:\customerdata\data.txt", True, , 65001
MsgBox "Done!"
End Sub
问题是,IIF并不适用于所有数据行,但它确实比较了作为字符串的Gender是否等于0整数,并且我的所有Gender行都变为2 ...... 生成的SQL如下:
SELECT T_Customer.[Name], 2 AS Gender, T_Customer.[Age] FROM T_Customer
需要帮助
解决!!!
通过在sql语句IN STRING中编写IIF,并使用相同的字段名称,写如下: IIf([tablemame]。[Gender] = 0,1,2)& "作为性别,"
答案 0 :(得分:2)
我认为你省略了两个引号。在对象循环中if之后的行应该是:
sql = sql & " IIf([Gender] = 0, 1, 2) " & " AS Gender, "
现在sql变量是
SELECT [Name], IIf([Gender] = 0, 1, 2) AS Gender, [Age] FROM T_Customer