With a for loop in VBA I would like to write values of an array into a table in ACCESS, but the array values has apostrophe, therefore the SQL is not working in VBA
Arr(1) = "Mother' love"
Arr(2) = "Father' love"
Arr(3) = "Value in 'Mother'"
Arr(4) = "Value in 'Father'"
For i = 1 To 4 Step 1
strSQL = "Insert into [Table1] (Content) Values (" & Arr(i) & ")"
Debug.Print strSQL
DoCmd.RunSQL strSQL
Application.RefreshDatabaseWindow
Next i
Errors as follow
Run-time error '3075'
Syntax error (missing operator) in query expression 'Mother's love)'
firstly thanks in advance.
Before posting this, I’ve searched and tried a few guides from several sources. However, was to no success in constructing formula string. Filtering data in Google sheet (GS) using Query + other functions is something new to me and I’m still learning.
Goal:
Use drop-down and 2 types of check-boxes (data type: text & True/False). User can select one or more checkboxes to filter data.
Problem:
Was not successful to construct formula string to combine drop-down & check-boxes
My GS example was taken from one of the solution which used a simple formula string for check-boxes (data type: True/False) to create filter and hoping I could developed from it.
My Google sheet link
Thanks again!
This seems to do what you've asked for. Try the following formula in E6 of your results sheet. Or see my tab Answer-GK, which I added to your sheet.
=iferror(if(or(C5:C13),
QUERY ('Test string DB'!A3:H12, "Select * WHERE " &
IF(B2="","C <>'' "," C = '"&B2&"'") &
IF(C5, " and D matches '" & B5 & "' ",) &
IF(C6, " and D matches '" & B6 & "' ",) &
IF(C7, " and D matches '" & B7 & "' ",) &
IF(C8, " and E matches '" & B8 & "' ",) &
IF(C9, " and E matches '" & B9 & "' ",) &
IF(C10, " and E matches '" & B10 & "' ",) &
IF(C11, " and F=TRUE", ) &
IF(C12, " and G=TRUE", ) &
IF(C13, " and H=TRUE", )),
QUERY ('Test string DB'!A3:H12,"select * " & if(B2=""," "," where C = '"&B2&"'") ,0)),
"No results - Reduce your selected criteria")
Something to note. Since some of your data elementss have dedicated columns, and others are shared columns, it limits what you can filter. For example, you can't find a campsite that has a beach AND a lake, since your data only allows you to have one or the other element.
The same for access - it can't be both walking and 4WD. So if you check two or more tickboxes, for either Features or Accessible By, you get no results. You don't have this issue with Ammenities, since you have a separate column (data element) for each of shower, toilet, and plugpoint. I hope this makes sense, I assume that you may want to expand your data structure to allow multiple elements, as you have for Amenities. The formula would need to be modified in that case.
But otherwise I think it is working as you intended.
Let us know of any issues with this.
UPDATE:
To perhaps improve the appearance, I modified The "TRUE" values to show as "Y", using hidden columns in J to L, and the following formula in M5. There are also other ways of doing this.
={"Toilet","Shower","Plugpoint";
arrayformula(if(J6:L,"Y",""))}
This adds the three header labels, then looks through columns J,K,L for TRUE values, and returns them as "Y"s.
I am assuming you intended the the Feature and Accessible check boxes to be inclusive and the Amenities to be exclusive. Here is a working example:
https://docs.google.com/spreadsheets/d/1AqQCKRrhTzJyLWldcpeFAv1o4sLvphv3F52VCgpa4rE/edit?usp=sharing
Here is the formula in E6:
=iferror(QUERY(QUERY('Teststring DB'!A3:H12,"select * where C = """&B2&""" AND (D = """& if(C5 = true,B5,"")&""" OR D = """& if(C6 = true,B6,"")&""" OR D = """& if(C7 = true,B7,"")&""") OR (E = """& if(C8 = true,B8,"")&""" OR E = """& if(C9 = true,B9,"")&""" OR E = """& if(C10 = true,B10,"")&""") ",0),"select * " & if(C11 = true,"where Col6 = true","")&" " & if(C12 = true,if(C11 = true,"AND Col7 = true","where Col7 = true"),"")&" " & if(C13 = true,if(or(C11 = true,C12 = true),"AND Col8 = true","where Col8 = true"),"")&" ",0),QUERY('Teststring DB'!A3:H12))
Fields
[Group] (values are ARTS , PRE-MED , PRE-ENG, ICS )
i want data on new field called RollNo something like 18AR01"
only 2 last digits of current year + iif([group]=ARTS,AR,iif([group]=PRE-MED,PM,iif([group]=PRE-ENG,PE,iif([group]=ICS,NULL,))))
how this can be possible ?
You can use the BeforeInsert event of the form:
Dim Code As String
Dim Number As Integer
Code = Nz(Format(Date, "yy") & Switch(Me![Group] = "ARTS", "AR", Me![Group]= "PRE-MED", "PM", Me![Group] = "PRE-ENG", "PE"))
Number = Val(Nz(DMax("Right([RollNo], 2)", "YourTable", "[RollNo] Like '" & Code & "??'"))) + 1
Me!RollNo.Value = Format(Date, "yy") & Code & Format(Number, "00")
I have FDQuery and DataSource and DBGrid. Now I write this code in Button
FDQuery2->Active = false;
FDQuery2->SQL->Clear();
FDQuery2->SQL->Add(" SELECT C.patient_id, P.patient_name, C.check_id "
" FROM Checkup C "
" INNER JOIN Patient P ON (C.patient_id=P.patient_id) "
" WHERE C.today = " + MaskEdit1->Text +
" ORDER BY C.check_id ");
FDQuery2->Active = true;
and i connect FDQuery to DataSource and tDataSource to DBGrid, but when I click the Button it doesn't show rows. and i am sure that SQL code is work, because when i write inside the SQL String the rows have been shown.
any ideas.
You're missing the ' around your value when concatenating the text. Change your WHERE clause:
FDQuery2->SQL->Clear();
FDQuery2->SQL->Add(" SELECT C.patient_id, P.patient_name, C.check_id "
" FROM Checkup C "
" INNER JOIN Patient P ON (C.patient_id=P.patient_id) "
" WHERE C.today = '" + MaskEdit1->Text + "'" +
" ORDER BY C.check_id ");
You really should learn to use parameterized queries instead, though. It allows the database driver to handle things like properly quoting text or formatting dates for you, and it also (importantly) prevents SQL injection.
FDQuery2->SQL->Clear();
FDQuery2->SQL->Add(" SELECT C.patient_id, P.patient_name, C.check_id "
" FROM Checkup C "
" INNER JOIN Patient P ON (C.patient_id=P.patient_id) "
" WHERE C.today = :today" +
" ORDER BY C.check_id ");
FDQuery2->ParamByName("today")->AsString = MaskEdit1.Text;
FDQuery2->Active = true;
How do you return an array from a sqlite database in lua?
I have this sameple code here:
function getMoveName()
tempMoveName = {}
for row in db:nrows("SELECT * FROM movetable") do
tempMoveName = row.movename .. " " .. row.totalcubicfeet .. " " .. row.totalitem .. "\n"
end
return tempMoveName
end
which will return the content of the database and then print the content with this line of code:
local displaymovenames = mydatabase.getMoveName()
print ( displaymovenames )
however it only returns the last data and not all of the contents of it.
What you are doing is for every row, you just store the row data in the variable tempMoveName and hence overwriting the previous value.
You need to add the rowData to the table tempMoveName.
function getMoveName()
tempMoveName = {}
for row in db:nrows("SELECT * FROM movetable") do
local rowData = row.movename .. " " .. row.totalcubicfeet.." "..row.totalitem.."\n"
tempMoveName[#tempMoveName+1] = rowData
end
return tempMoveName
end
EDIT
To access the table elements you have to do the following
for i=1,#tempMoveName do
print(tempMoveName[i])
end
note that #tempMoveName gives the length of the table(i.e no. of elements in the table)
P.S if you are going to do a lot of coding in Lua, I'd suggest you get a grip on the basics of Tables, cos table is the main datatype of Lua. Arrays, lists, dictionaries, classes and almost everything are implemented via tables. Here is a tutorial for a start!
This has nothing to do with SQL or databases; this is basic Lua stuff here.
This:
tempMoveName = {}
creates a table and stores the table into the (global) variable named tempMoveName.
This:
tempMoveName = row.movename .. " " .. row.totalcubicfeet .. " " .. row.totalitem .. "\n"
Creates a big string from concatenating other strings (don't forget about string.format) and stores that in the (global) variable named tempMoveName.
Note what I said: "stores it in the variable". Not "in the table stored in the variable".
It's no different from doing this:
tempMoveName = 1
tempMoveName = "foo"
This doesn't combine a string with an integer in some way. tempMoveName held 1, then it was replaced with "foo".
Tables aren't special; they're values just like anything else in Lua. Variables hold values. So if you change which value is stored in a variable, you have changed which value is stored there. You haven't affected the value itself, only where it is stored.
If you have a table stored in a variable, and you want to build an array, you should access the elements within the table, not the variable itself. This is done in the usual way:
tempMoveName[#tempMoveName + 1] = row.movename .. " " .. row.totalcubicfeet .. " " .. row.totalitem .. "\n"
Alternatively, you can use table.insert:
table.insert(tempMoveName, row.movename .. " " .. row.totalcubicfeet .. " " .. row.totalitem .. "\n")
Lastly, if tempMoveName is meant to be temporary (as the name suggests), then you should declare it as a local variable.