0

I need to import data from excel to access. The importation is now "working" for the obvious types (string, integers). However, In the excel file i have some strings that i need to convert to boolean in my access tables. The strings can take only 2 values "oui" or "non" (yes or no in french). In my vba code i have the following line:

cSQL = "INSERT INTO " & strTable & " ( [N_CLIENT], [NOM_CLI], ) VALUES (" & Chr(34) & .Range("A" & i) & Chr(34) & "," & Chr(34) & .Range("F" & i) & Chr(34) & ");"
        
                DoCmd.RunSQL cSQL

I would liketo know if i can use an if condition to check the value itself inside the cSQL call and replace it with either true or false. like what follows.

cSQL = "INSERT INTO " & strTable & " ( [N_CLIENT], [NOM_CLI], ) VALUES (If(" & Chr(34) & .Range("A" & i) & Chr(34) & " = "oui") then "true" else then "false"," & Chr(34) & .Range("F" & i) & Chr(34) & ");"
        
                DoCmd.RunSQL cSQL
4

4 回答 4

1

您还可以使用 SQL 中的 IIF 语句来检查值:

http://msdn.microsoft.com/en-us/library/hh213574.aspx

于 2013-07-31T12:17:53.893 回答
1

您可以在 Access-SQL-Queries 中使用 VBA 函数:

"[...] VALUES(strcomp('" & Range("A" & i) & "','oui', 1)=0 [...]"

问候,

AKDA

于 2013-07-31T10:59:05.633 回答
1

看起来您希望 Access SQL 将您的 oui / non 评估为真假。您可以使用 iif 语句http://www.techonthenet.com/access/functions/advanced/iif.php来做到这一点。然而,何苦呢?您可以只评估 vba 中的范围并传递布尔变量。使用这样的东西:

dim result as boolean
if sheet1.Range("A" & i) = "oui" then
    result = true
else
    result = false
end if

然后只需将其插入到您的 SQL 中:

cSQL = "INSERT INTO " & strTable & " ( [N_CLIENT], [NOM_CLI], ) VALUES (If(" & Chr(34) & result & Chr(34) & , & Chr(34) & .Range("F" & i) & Chr(34) & ");"
于 2013-07-31T12:08:15.957 回答
0

使用撇号比使用 Chr(34) 更容易,并且使用续行字符“_”将语句分成几行有助于阅读该语句。该IIf()功能也用于以下。

另请注意,术语 [NOM_CLI] 后面有一个额外的逗号,它不应该存在。

    cSQL = "INSERT INTO " & strTable & "( [N_CLIENT], [NOM_CLI] ) VALUES ('" _
    & IIf(StrComp(.Range("A" & i), "oui", 1) = 0, "true", "false") _
    & "','" & .Range("F" & i) & "');"

这会产生这样的字符串:

INSERT INTO ( [N_CLIENT], [NOM_CLI] ) VALUES ('true','hello');

要使用布尔值 (0, -1) 更改为:

    cSQL = "INSERT INTO " & strTable & "( [N_CLIENT], [NOM_CLI] ) VALUES (" _
    & IIf(StrComp(.Range("A" & i), "oui", 1) = 0, -1, 0) _
    & ",'" & .Range("F" & i) & "');"

产生

INSERT INTO ( [N_CLIENT], [NOM_CLI] ) VALUES (-1,'hello');
于 2013-07-31T12:12:05.267 回答