Convert strings to booleans - excel

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

You can use VBA-Functions in Access-SQL-Queries:
"[...] VALUES(strcomp('" & Range("A" & i) & "','oui', 1)=0 [...]"
Regards,
AKDA

It looks like you want the Access SQL to evaluate your oui / non into true and false. You could do this using the iif statement http://www.techonthenet.com/access/functions/advanced/iif.php. However, why bother? You could just evaluate the range in vba and pass through the boolean variable. Using something like this:
dim result as boolean
if sheet1.Range("A" & i) = "oui" then
result = true
else
result = false
end if
Then just insert that into your SQL:
cSQL = "INSERT INTO " & strTable & " ( [N_CLIENT], [NOM_CLI], ) VALUES (If(" & Chr(34) & result & Chr(34) & , & Chr(34) & .Range("F" & i) & Chr(34) & ");"

You could also use the IIF statement in SQL to check the value:
http://msdn.microsoft.com/en-us/library/hh213574.aspx

It is easier to use apostrophes rather than Chr(34), and splitting the statement across lines using the line-continuation character '_' helps to read the statement. The IIf() function is also used in the following.
Also note that you have an extra comma after the term [NOM_CLI] which shouldn't be there.
cSQL = "INSERT INTO " & strTable & "( [N_CLIENT], [NOM_CLI] ) VALUES ('" _
& IIf(StrComp(.Range("A" & i), "oui", 1) = 0, "true", "false") _
& "','" & .Range("F" & i) & "');"
This results in a string like this:
INSERT INTO ( [N_CLIENT], [NOM_CLI] ) VALUES ('true','hello');
To use the Boolean values (0, -1) change to:
cSQL = "INSERT INTO " & strTable & "( [N_CLIENT], [NOM_CLI] ) VALUES (" _
& IIf(StrComp(.Range("A" & i), "oui", 1) = 0, -1, 0) _
& ",'" & .Range("F" & i) & "');"
which yields
INSERT INTO ( [N_CLIENT], [NOM_CLI] ) VALUES (-1,'hello');

Related

Inserting formula as string into a cell

It seems to be a problem with some characters in it
My original formula in the worksheet (working):
ThisWorkbook.Worksheets("Zazmluvnenia").Cells("3", "AM").Value = "=Výkony!M4 & ", " & TEXT(Výkony!J4;"d.m.yyyy") & ", " & TEXT(Výkony!K4;"hh:mm")"
Putting this into VBA doesn't work because of the quotes. I tried to solve the problem this way: How do I put double quotes in a string in vba?
I changed my code according to the answers on the question above:
"=Výkony!M3 & " & Chr(34) & ", " & Chr(34) & " & TEXT(Výkony!J3;" & Chr(34) & "d.m.yyyy" & Chr(34) & ") & " & Chr(34) & ", " & Chr(34) & " & TEXT(Výkony!K3;" & Chr(34) & "hh:mm" & Chr(34) & ")"
And error '1004' occurred.
Double Quotes vs CHR(34)
Your CHR solution was almost fine, but you didn't know that VBA doesn't recognize the semi-colon (;) as a separator like Excel does. You always have to replace every semi-colon (used as a separator) with a comma in VBA.
Similarly VBA uses the dot (.) as the decimal separator.
The Code
Option Explicit
Sub SEMI()
Dim strF As String
' Double double-quotes ("") in several rows.
strF = "=Výkony!M4&"", """ _
& "&TEXT(Výkony!J4,""d.m.yyyy"")&"", """ _
& "&TEXT(Výkony!K4,""hh:mm"")"
' CHR(34) in several rows.
strF = "=Výkony!M4&" & Chr(34) & ", " & Chr(34) _
& "&TEXT(Výkony!J4," & Chr(34) & "d.m.yyyy" & Chr(34) & ")&" _
& Chr(34) & ", " & Chr(34) _
& "&TEXT(Výkony!K4," & Chr(34) & "hh:mm" & Chr(34) & ")"
' Double double-quotes ("") in one row.
strF = "=Výkony!M4&"", ""&TEXT(Výkony!J4,""d.m.yyyy"")&"", ""&TEXT(Výkony!K4,""hh:mm"")"
' CHR(34) in one row.
strF = "=Výkony!M4&" & Chr(34) & ", " & Chr(34) & "&TEXT(Výkony!J4," & Chr(34) & "d.m.yyyy" & Chr(34) & ")&" & Chr(34) & ", " & Chr(34) & "&TEXT(Výkony!K4," & Chr(34) & "hh:mm" & Chr(34) & ")"
ThisWorkbook.Worksheets("Zazmluvnenia").Cells("3", "AM").Value = strF
End Sub

What format do I use for a string variable in a query

I am performing a web query and want to filter a column by a cell reference.
ThisWeek = ThisWorkbook.Sheets("Summary").Range("A1").Value
LastWeek = ThisWorkbook.Sheets("Summary").Range("M1").Value
ActiveWorkbook.Queries.Add Name:="1245", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source =
Json.Document(Web.Contents(""" & finalstr & """))," & Chr(13) & "" &
Chr(10) & " each ([Column1.report_begin_date] = " < " & ThisWeek & " > "
or [Column1.report_begin_date] = " < " & Last Week & " > ")"
#""Filtered Rows"""
ThisWeek and LastWeek are both strings that change each day and I want the query to filter out by these dates. VBA doesn't recognize the way Im formatting my variable names and doesn't recognize them as an editable variable.
Basically what you want to do is add quotation marks for your date string variable, correct?
You can achieve that by using: """" (4 times quotation marks)
Which will create a simple "
So if you add it to both sides of your string variable, you will get what you need, as follows:
[...] " < " & """" & ThisWeek & """" & [...]
Hope this helps

1004 error when writing a formula to a cell using VBA-Excel

I am trying to write a formula into a cell, using VBA. The code excerpt below delivers the correct formula, with correct references. But I keep getting a 1004 run-time error. I can't figure out what is triggering it. Hope this code excerpt is enough to reveal the answer, but if you need more just ask.
Set rg_sheet = ActiveWorkbook.Worksheets("RUNGLANCE")
for colNo = 2 to 8
for rowNo = 3 to 27
daySheetName = Cells(1,colNo)
rg_sheet.cells(rowNo,ColNo).formula = "=VLookUp(" & Chr(34) & "$A" & rowNo & Chr(34) & "), " & daySheetName & "!(" & chr(34) & "$A$3:$B$27" & Chr(34) & ", 2, False"
next rowNo
next colNo
Instead of
rg_sheet.cells(rowNo,ColNo).formula = "=VLookUp(" & Chr(34) & "$A" & rowNo & Chr(34) & "), " & daySheetName & "!(" & chr(34) & "$A$3:$B$27" & Chr(34) & ", 2, False"
try
rg_sheet.Cells(rowNo, colNo).Formula = "=VLookUp($A" & rowNo & ", " & daySheetName & "!" & "$A$3:$B$27" & ", 2, False" & ")"

Excel VBA #SQL String filter

I have been struggling on making this statement a NOT statement:
strfilter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%undeliverable%'"
this strfilter is being used in a items.restrict(strfilter), so obviously it's checking for all emails that contains the word undeliverable.
What I need to do is convert this statement so that it will EXCLUDE all emails with the subject containing the Words "undeliverable".
I've tried this but it returned a parse error:
"strFilter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " NOT '%undeliverable%'"
thanks in advance.
The Correct syntax is
Filter = "#SQL=" & " Not " & _
"urn:schemas:httpmail:subject" & "" & _
" Like '%undeliverable%'"
Or I prefer this then Like
strFilter = "#SQL=" & " Not " & _
"urn:schemas:httpmail:subject" & "" & _
" ci_phrasematch 'undeliverable'"

Single quote (') or backslash (/) is causing my program a syntax error. Visual Studio

everything is working properly but when I entered (25's/Box) on my id textbox it prompts" Syntax error (missing operator) in query expression "25's/Box'.
I know it has to be the single quote thats causing the error. How am I gonna fix it?
it = dgvItems.Rows(i).Cells(1).Value
qt = dgvItems.Rows(i).Cells(2).Value
un = Convert.ToString(dgvItems.Rows(i).Cells(3).Value)
id = Convert.ToString(dgvItems.Rows(i).Cells(4).Value)
uc = dgvItems.Rows(i).Cells(5).Value
tc = dgvItems.Rows(i).Cells(6).Value
oledbSql.CommandText = "INSERT INTO Items(ResoNo, ItemNo, Qty, Unit, ItemDesc, UnitCost, TotalCost) VALUES('" _
& rn & "'," & it & "," & qt & ",'" & un & "','" & id & "'," & uc & "," & tc & ")"
oledbSql.ExecuteNonQuery()
I used this instead... its working... thank you
Dim ite As New OleDb.OleDbCommand("INSERT INTO Items(ResoNo, ItemNo, Qty, Unit, ItemDesc, UnitCost, TotalCost) VALUES('" _
& rn & "'," & it & "," & qt & ",'" & un & "',#id," & uc & "," & tc & ")", dbCon)
ite.Parameters.AddWithValue("#id", id)
ite.ExecuteNonQuery()

Resources