I have the following formula that I am inputting in a function.
dim minfee, feetier3, feetier4, feetier5, bpspread1, bpsread2, bpspread3 as double
call insformulaincell("=IF(K2 = 100, ""#NA"", IF(K2 <" & minfee & "," & feetier3 & ",IF(K2<" & feetier4 & ",K2+ " & bpspread1 & ",IF(K2<" & feetier5 & ",K2+ " & bpspread2 & ",K2+ " & bpspread3 & "))))")
'all the function does is paste the formula into a cell
'How would I format the formula so that it can be stored as a single string?
'Ex:
dim sFormula as string
sformula = ""=IF(K2 = 100, ""#NA"", IF(K2 <" & minfee & "," & feetier3 & ",IF(K2<" & feetier4 & ",K2+ " & bpspread1 & ",IF(K2<" & feetier5 & ",K2+ " & bpspread2 & ",K2+ " & bpspread3 & "))))""
call insformulaincell(sFormula)
The main issue is that the variables such as minfee would not reference its actual values but instead have the actual string variable name appear.
Ex: "... If(K2 <" & minfee & "," ... )
as opposed to
"... If(K2 < 1) ..." ' assuming that minfee = 1
In VBA " serves as a delimiter for string literals, like this:
Dim foo As String
foo = "some string"
If your string literal needs to contain " double quotes, you need to escape them, by doubling them up between the string delimiters:
foo = """some string"""
Printing the above would yield "some string", including the double quotes.
So you do need to remove the leading & trailing double quotes.
sformula = "=IF(K2 = 100, ""#NA"", IF(K2 <" & minfee & "," & feetier3 & ",IF(K2<" & feetier4 & ",K2+ " & bpspread1 & ",IF(K2<" & feetier5 & ",K2+ " & bpspread2 & ",K2+ " & bpspread3 & "))))"
Breaking this down, it's a concatenation of the following literals:
"=IF(K2 = 100, ""#NA"", IF(K2 <" (note, "#NA" is a bad idea IMO. Use the NA() function to yield an actual worksheet error instead of a string value that looks like one)
","
",IF(K2<"
",K2+ "
",IF(K2<"
",K2+ "
",K2+ "
"))))"
Which looks right to me.
Arguably, such concatenation is annoyingly confusing. A custom StringFormat function can help mitigate this, by abstracting away the concatenations:
sFormula = StringFormat("=IF(K2=100, ""#NA"", IF(K2<{0},{1},IF(K2<{2},K2+{3},IF(K2<{4},K2+{5},K2+{6}", _
minfee, feetier3, feetier4, bpspread1, feetier5, bpspread2, bpspread3)
Related
I've been to dozens of sites. None address my particular question. All (including official Microsoft) tell me to do what I'm doing.
Dim strSQL As String
strSQL = """INSERT INTO tblVolunteers " & vbCrLf & _
"VALUES (" & [txtTitle] & "," & [txtFirstName] & "," & [txtMiddle] & "," & [txtLastName] & "," & [txtEmail] & _
"," & [txtPhone] & "," & [txtChurch] & "," & [txtGroup] & "," & [txtCouncil] & "," & [chkParCo] & "," & _
[txtMailAdd] & ");"""
CurrentDb.Execute strSQL
Here's what Microsoft has to say:
Run-time error '3078'
The Microsoft Access database engine cannot find the input table or query ""INSERT INTO tblVolunteers
VALUES (Mr.,John,L.,Smith,jlsmith#email.com,800-555-1212,St. Smith's,Smith,1234,-1,10 Smith St.
Smithville, TX 77777-3333);"". Make sure it exists and that its name is spelled correctly.
Why is it looking for a table or query when not only have I specified VALUES but it has picked up all the values from the form?
You could either use my function CSql and concatenate the values like this:
strSQL = "INSERT INTO tblVolunteers " & _
"VALUES (" & CSql([txtTitle]) & "," & CSql([txtFirstName]) & "," & CSql([txtMiddle]) & "," & _
CSql([txtLastName]) & "," & CSql([txtEmail]) & "," & CSql([txtPhone]) & "," & CSql([txtChurch] & "," & _
CSql([txtGroup]) & "," & CSql([txtCouncil]) & "," & CSql([chkParCo]) & "," & CSql([txtMailAdd]) & ");"
or you could skip this mess and use DAO for much cleaner coding and easier debugging:
Dim Records As DAO.Recordset
Dim Sql As String
Sql = "Select * From tblVolunteers"
Set Records = CurrentDb.OpenRecordset(Sql, dbOpenDynaset, dbAppendOnly)
Records.AddNew
Records!Title.Value = Me!txtTitle.Value
Records!FirstName.Value = Me!txtFirstName.Value
Records!Middle.Value = Me!txtMiddle.Value
Records!LastName.Value = Me!txtLastName.Value
Records!Email.Value = Me!txtEmail.Value
Records!Phone.Value = Me!txtPhone.Value
Records!Church.Value = Me!txtChurch.Value
Records!Group.Value = Me!txtGroup.Value
Records!Council.Value = Me!txtCouncil.Value
Records!ParCo.Value = Me!chkParCo.Value
Records!MailAdd.Value = Me!txtMailAdd.Value
Records.Update
Records.Close
Basically you need double quotes qaround the text, so for that you can use CHR(34)
strSQL = "INSERT INTO tblVolunteers " & vbCrLf & _
"VALUES (" & CHR(34) & [txtTitle] & CHR(34) & "," & CHR(34) & [txtFirstName] & CHR(34) & "," & CHR(34) & [txtMiddle] & CHR(34) & "," & CHR(34) & [txtLastName] & CHR(34) & "," & CHR(34) & [txtEmail] & CHR(34) & _
"," & CHR(34) & [txtPhone] & CHR(34) & "," & CHR(34) & [txtChurch] & CHR(34) & "," & CHR(34) & [txtGroup] & CHR(34) & "," & CHR(34) & [txtCouncil] & CHR(34) & "," & CHR(34) & [chkParCo] & CHR(34) & "," & CHR(34) & _
[txtMailAdd] & CHR(34) & ");"
use Access Query Design View..... start with just a single field, and then build field by field...
you can toggle it to SQl View to see the syntax
I'm always getting the "enter parameter value error" when I try to run this code. The error refers to idPat, idSP, idPar and measure_value.
I have checked with the debug idPar, idPat, idSP and measure_value and they assume the correct value; the errors comes out when I run sql.
Dim idPar As Integer
Dim idPat As Integer
Dim idSP As Integer
Dim current_fc As String
Dim namePar As String
Dim sql As String
Dim measureValue As Integer
current_fc = TempVars!CurrentUsername
namePar = Me.cmbManualDailyPar.Value
measureValue = Me.txtMeasureValue
idPar = Nz(DLookup("ID_par", "Parameter", "name_par = '" & namePar & "'"), 0)
idPat = Nz(DLookup("ID_patient", "Patient", "fiscal_code = '" & current_fc & "'"), 0)
idSP = Nz(DLookup("ID_SP", "Diagnosis", "ID_patient = " & idPat & ""), 0)
sql = "INSERT INTO Measure(ID_par, measure_value,ID_SP,ID_patient)" & _
" VALUES(idPar,measureValue,idSP,idPat);"
DoCmd.RunSQL sql
Your VALUES part is just text. What you need is:
" VALUES(" & idPar & "," & measureValue & "," & idSP & "," & idPat & ")"
You may also need to enclose text values in database quotes, for example:
" VALUES(" & idPar & ", '" & measureValue & "'," & idSP & "," & idPat & ")"
to enclose measureValue if it is text.
I am trying to create a basic data entry form, however, it is turning into more trouble than I anticipated.. I have the form created, now I am just trying to INSERT the data into the DB (TEST). I am receiving an "Object Required" error. Any suggestions? All of the txt boxes are verified to be correct. This is all being done through Access VBA
Private Sub Command28_Click()
Dim sSQL As String
Set sSQL = "INSERT INTO TEST (Carrier_Ent_ID, Row_Insert_TS, Row_Update_TS,
Row_Insert_User_ID, Row_Update_User_ID, Carrier_Ent_Name, Active)
VALUES (" & Me.txtENTID & "," & Me.txtDate & "," & Me.txtDate & "," &
Me.cmboUserID & "," & Me.cmboUserID & "," & Me.txtENTNAME & ","
& Me.Txtactive & "); "
DoCmd.RunSQL.sSQL
End Sub
From what I can see there's a couple of mistakes in the code.
You only use SET when setting a reference to an object - sSQL is a text string.
DoCmd.RunSQL shouldn't have a full-stop after RunSQL- just the text string.
If Me.txtENTNAME is a text string it should have an apostrophe before and after it.
Private Sub Command28_Click()
Dim sSQL As String
'No Set & ' before and after Me.txtENTNAME (assuming it's text).
sSQL = "INSERT INTO TEST (Carrier_Ent_ID, Row_Insert_TS, Row_Update_TS, " & _
"Row_Insert_User_ID, Row_Update_User_ID, Carrier_Ent_Name, Active) " & _
"VALUES (" & Me.txtENTID & "," & Me.txtDate & "," & Me.txtDate & "," & _
Me.cmboUserID & "," & Me.cmboUserID & ",'" & Me.txtENTNAME & "'," & Me.txtActive & "); "
'No full stop after RunSQL.
DoCmd.RunSQL sSQL
End Sub
I have this SQL string
theSQL = "INSERT INTO tbl_PROJECTS (co_id, contact_id, prop_id, worktype_id, incharge_id, project_name, project_ref," _
& "project_status, project_awardref, project_awarddate, project_startdate, project_targetdate," _
& "project_completedate, project_finalreportdate, project_location, project_notes)"
theSQL = theSQL & " VALUES (" & theCoID & "," & theContactID & "," & thePropID & "," & theWorkTypeID & "," & theInCharge & "," _
& "'" & theProjectName & "'" & "," & theProjectRef & "," & theProjectStatus & "," & theAwardRef _
& theAwardDate & "," & theStartDate & "," & theTargetDate & "," & theCompleteDate & "," & theFinalReportDate & "," _
& theLocation & "," & theNotes & ")"
When I do DoCmd.RunSQL (theSQL) I get syntax error (runtime error 3134).
I sent the output to Debug.Print. Can't find what is wrong with the syntax.
Anyone who can tell what is wrong with this sql command from VBA?
Some variables are null like thetargetdate and thecompletedate and I did not include the projectID in this query because it is autonumber. I want the number generated automatically.
Is it not allowed to pass null value to SQL?
Thanks
DEBUG PRINT RESULT :
INSERT INTO tbl_PROJECTS (co_id, contact_id, prop_id, worktype_id,
incharge_id, project_name, project_ref, project_status,
project_awardref, project_awarddate, project_startdate,
project_targetdate, project_completedate, project_finalreportdate,
project_location, project_notes) VALUES (61,66,134,1,1,'STRUCTURAL
DESIGN',,AWARDED,Test LPO,2/11/2016,,,,,Dnata 4 storey warehouse,)
Is it not allowed to pass null value to SQL?
It is, but it can't be done by stating a space. You must write Null:
VALUES (61,66,134,1,1,'STRUCTURAL DESIGN',Null,'AWARDED','Test LPO',#2/11/2016#,Null,Null,Null,Null,'Dnata 4 storey warehouse Alquoz',Null)
and quotes and date delimiters are missing.
You wrote this:
theSQL = "INSERT INTO tbl_PROJECTS (co_id, contact_id, prop_id, worktype_id, incharge_id, project_name, project_ref," _
& "project_status, project_awardref, project_awarddate, project_startdate, project_targetdate," _
& "project_completedate, project_finalreportdate, project_location, project_notes)"
theSQL = theSQL & " VALUES (" & theCoID & "," & theContactID & "," & thePropID & "," & theWorkTypeID & "," & theInCharge & "," _
& "'" & theProjectName & "'" & "," & theProjectRef & "," & theProjectStatus & "," & theAwardRef _
& theAwardDate & "," & theStartDate & "," & theTargetDate & "," & theCompleteDate & "," & theFinalReportDate & "," _
& theLocation & "," & theNotes & ")"
In the second line in the second block, there's no ending quotes.
This is a simple question. I have this code:
CurrentRow = 3
MyColumn = 2
CurrentCell = (CurrentRow & "," & MyColumn)
WorkingSheet.Cells(CurrentCell).value = (ClientName & " " & "(" & ClientLocation & ")" & " " & ExtraDefinition)
I thought that this would place the data on the 'WorkingSheet' in the position "B3" but it places the data in the cell "AF1".
Why is this?
Thanks,
Josh
Cell is not expected to be used as you are using it; you have to input the row and column indices (as integers) separated by a comma. Thus the right code is:
WorkingSheet.Cells(CurrentRow, MyColumn).Value = (ClientName & " " & "(" & ClientLocation & ")" & " " & ExtraDefinition)
Another alternative you have is using Range. Example:
WorkingSheet.Range("B3").Value = (ClientName & " " & "(" & ClientLocation & ")" & " " & ExtraDefinition)