strSQL = "INSERT INTO tbl_Processor_Info ([Division],[Service_Area],[Service_Line],[Service_Line_Process],[SLA_Type],[SLA_Ref],[Processing_Date],[Total_Processed],[Onshore_Error],[Invalid_Queries],[TAT_Missed],[RFT_Error],[Techinician]) VALUES('" & _
.Range("M" & i) & "','" & .Range("I" & i) & "','" & .Range("J" & i) & "','" & .Range("K" & i) & "','" & .Range("N" & i) & "','" & .Range("B" & i) & "',#" & .Range("A" & i) & "#," & .Range("C" & i) & "," & .Range("E" & i) & "," & .Range("F" & i) & "," & .Range("G" & i) & "," & .Range("H" & i) & ",'" & Environ("Username") & "')"
results in:
INSERT INTO tbl_Processor_Info ([Division],[Service_Area],[Service_Line],[Service_Line_Process],[SLA_Type],[SLA_Ref],[Processing_Date],[Total_Processed],[Onshore_Error],[Invalid_Queries],[TAT_Missed],[RFT_Error],[Techinician]) VALUES('ATA','PUT','DE','PBR','','SP-154',#02/01/2020#,4,,,1,,'arathod')
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 need to add 111 days to my STDATE but it stops and give an error after it reaches the 12th day:
Conversion failed when converting date and/or time from character string.
However, if I add 15 months to the same STDATE it works perfectly.
I = 0
For Z = 0 To 111
If CTY = "D" Then
STDATE = STDATE.AddDays(I)
'STDATE2 = STDATE.ToString("dd-MM-yyyy 00:00:01")
PAMTPART = Math.Round(TB3.Text / INSTN, 2)
CINSTUNIQ = Guid.NewGuid()
INTAMTPART = Math.Round(TB4.Text / INSTN, 2)
INSTSTR = "INSERT INTO [dbo].[tb_CaseInst] ([CaseInstUniq],[CaseInst_OrgUniq],[CaseInst_CaseUniq],[CaseInstDueDate],[CaseInstAmount], [CaseInstBalAmt],[CaseInstStatus],[CaseInstRemks],[CaseInstDate],[CaseInstPAmt],[CaseInstInttAmt],[IsActive])" _
& "VALUES ('" & CINSTUNIQ.ToString & "','" & ORGUNQ.ToString & "','" & CSUNIQ.ToString & "','" & STDATE & "'," & INSTAMT & "," & INSTAMT & ",'D','Inst. Due','" & SRDATE & "'," & PAMTPART & "," & INTAMTPART & ",'A')"
CINSTSTR = "INSERT INTO [dbo].[tb_CaseInstD] ([CaseInstDUniq],[CaseInstD_OrgUniq],[CaseInstD_CaseUniq],[CaseInstD_InstUniq],[CaseInstDTranDueDate],[CaseInstDAmt],[CaseInstDBalAmt],[CaseInstDStatus],[CaseInstDRemks],[CaseInstDChqNo],[CaseInstDBankName],[CaseInstDChqDate],[CaseInstDChqAmt],[CaseInstDDate],[CaseInstDIsActive],[CaseInstDTransType],[CaseInstDPAmt],[CaseInstDInttAmt],[CaseInstDType])" _
& "VALUES ('" & Guid.NewGuid().ToString & "','" & ORGUNQ.ToString & "','" & CSUNIQ.ToString & "','" & CINSTUNIQ.ToString & "','" & STDATE & "'," & INSTAMT & "," & INSTAMT & ",'D','Inst. Due','XX','XX','" & SRDATE & "',0,'" & SRDATE & "','A'," & CASEI & "," & PAMTPART & "," & INTAMTPART & ",'I')"
I = 1
End If
I think that you have to use DateAdd function with DateInterval.Day,1,STDATE parameters :
STDATE = DateAdd(DateInterval.Day, 1, STDATE )
Hope this help you
i am very new to VBA or programmin in general.
Nevertheless, I need to create a code for exporting a csv file.
My issue is that I have several rows with varying lenght in my file.
The file looks as following:
3,6,8
3,9,4,7,8
1
2,4
7,9,4,5,6,8,1
3
My codes gives me:
"3","6","8","","","",""
"3","9","4","7","8","",""
"1","","","","","",""
"2","4","","","","",""
"7","9","4","5","6","8","1"
"3","","","","","",""
I need:
"3","6","8"
"3","9","4","7","8"
"1"
"2","4"
"7","9","4","5","6","8","1"
"3"
Can anyone please help me?
Sub CIF_Katalog_2()
Dim i As Long, lngZeile As Long
Dim myCSVFileName As String
Dim myWB As Workbook
Set myWB = ThisWorkbook
myCSVFileName = myWB.Path & "\" & "CSV Katalog Export_" & VBA.Format(VBA.Now, "dd-MMM-yyyy hh-mm") & ".csv"
lngZeile = Range("A" & Rows.Count).End(xlUp).Row
Open myCSVFileName For Output As #1
For i = 1 To lngZeile
If i = 1 Then
Print #1, Chr(34) & Cells(i, 1).Value & Chr(34)
ElseIf i = 2 Then
Print #1, Chr(34) & Cells(i, 1).Value & Chr(34) & "," & Chr(34) & Cells(i, 2).Value & Chr(34)
Else
Print #1, Chr(34) & Cells(i, 1).Value & Chr(34) & "," & Chr(34) & Cells(i, 2).Value & Chr(34) & "," & Chr(34) & Cells(i, 3).Value & Chr(34) & "," & Chr(34) & Cells(i, 4).Value & Chr(34) & "," & Chr(34) & Cells(i, 5).Value & Chr(34) & "," & Chr(34) & Cells(i, 6).Value & Chr(34) & "," & Chr(34) & Cells(i, 7).Value & Chr(34) & "," & Chr(34) & Cells(i, 8).Value & Chr(34) & "," & Chr(34) & Cells(i, 9).Value & Chr(34) & "," & Chr(34) & Cells(i, 10).Value & Chr(34) & "," & Chr(34) & Cells(i, 11).Value & Chr(34) & "," & Chr(34) & Cells(i, 12).Value & Chr(34) & "," & Chr(34) & Cells(i, 13).Value _
& Chr(34) & "," & Chr(34) & Cells(i, 14).Value & Chr(34) & "," & Chr(34) & Cells(i, 15).Value & Chr(34) & "," & Chr(34) & Cells(i, 16).Value & Chr(34) & "," & Chr(34) & Cells(i, 17).Value & Chr(34) & "," & Chr(34) & Cells(i, 18).Value & Chr(34) & "," & Chr(34) & Cells(i, 19).Value & Chr(34) & "," & Chr(34) & Cells(i, 20).Value & Chr(34) & _
"," & Chr(34) & Cells(i, 21).Value & Chr(34) & "," & Chr(34) & Cells(i, 22).Value & Chr(34) & "," & Chr(34) & Cells(i, 23).Value & Chr(34) & "," & Cells(i, 24).Value & Chr(34) & "," & Chr(34) & Cells(i, 25).Value & Chr(34) & "," & Chr(34) & Cells(i, 26).Value & Chr(34) & "," & Chr(34) & Cells(i, 27).Value & Chr(34) & "," & Chr(34) & Cells(i, 28).Value & Chr(34) & "," & Chr(34) & Cells(i, 29).Value & Chr(34) & "," & Cells(i, 30).Value & "," & Chr(34) & Cells(i, 31).Value & Chr(34) & "," & Chr(34) & Cells(i, 32).Value & Chr(34) & "," & Chr(34) & Cells(i, 33).Value & Chr(34) & "," & Chr(34) & Cells(i, 34).Value & Chr(34) & "," & Chr(34) & Cells(i, 35).Value & Chr(34)
End If
Next i
Close #1
MsgBox "Erledigt"
End Sub
Give this a try... just reusing your code:
Sub CIF_Katalog_2()
Dim i As Long, x As Long, lngZeile As Long
Dim myCSVFileName As String, strTemp As String
Dim myWB As Workbook: Set myWB = ThisWorkbook
myCSVFileName = myWB.Path & "\" & "CSV Katalog Export_" & VBA.Format(VBA.Now, "dd-MMM-yyyy hh-mm") & ".csv"
lngZeile = Range("A" & Rows.Count).End(xlUp).Row
Open myCSVFileName For Output As #1
For i = 1 To lngZeile
For x = 1 To 35
If Cells(i, x) <> "" Then
strTemp = strTemp & Chr(34) & Cells(i, x).Value & Chr(34) & ","
End If
Next x
Print #1, Left(strTemp, Len(strTemp) - 1)
strTemp = ""
Next i
Close #1
MsgBox "Erledigt"
End Sub
Hey everyone I keep getting the following error, Run-Time error '3075': syntax error in string in query expression "0,")'
CurrentDb.Execute "INSERT INTO MobileDeviceUserList(sUser, sDevice, sMobileNumber, sIMEINumber, sSerialNumber, sAccessories, sNewSIMNumber, sComments, sRecorded) " & _
"VALUES (" & Me.cboDevice & ",'" & Me.cboAccessories & ",'" & Me.txtUser & ",'" & Me.txtNumber & ",'" & Me.txtIMEI & ",'" & Me.txtSerial & ",'" & Me.txtNEWSIM & ",'" & Me.recRecorded & ",'" & _
Me.txtComment & "')"
Start pressing F8 until you reach the problematic string
Press Ctrl + G.
Write exactly this on what you see:
?"INSERT INTO MobileDeviceUserList(sUser, sDevice, sMobileNumber, sIMEINumber, sSerialNumber, sAccessories, sNewSIMNumber, sComments, sRecorded) " & _
"VALUES (" & Me.cboDevice & ",'" & Me.cboAccessories & ",'" & Me.txtUser & ",'" & Me.txtNumber & ",'" & Me.txtIMEI & ",'" & Me.txtSerial & ",'" & Me.txtNEWSIM & ",'" & Me.recRecorded & ",'" & _
Me.txtComment & "')"
The "Debugging" should be quite easy now and the error must be evident.
You are misplacing ticks (e.g. ' ) in the string construction. Probably through copy and paste errors.
Example:
... & ",'" & Me.cboAccessories & ",'" & Me.txtUser & ",'" & Me.txtNumber & ",'" & ...
... should be something closer to,
... & ",'" & Me.cboAccessories & '",'" & Me.txtUser & '"," & Me.txtNumber & ",'" & ...
I cannot supply a foolproff answer as I do not know what fields are text, numeric, dates or memos. Maybe do a string construction into a string var and debug.print the var to see the result.
I need a .FormulaArray inserted into my worksheet, however it is well over the 255 character limit (it's 420). I've tried the .Replace workaround, but it doesn't work. Using .Formula = will get it into the cell, but it doesn't calculate and produces #VALUE!, so I would have to crtl+shift+Enter manually. Is there a way VBA can replicate this?
.Cells(Application.WorksheetFunction.Match("Red Car", .Range("A:A"), 0), Application.WorksheetFunction.Match(ComboBox1.Value & " " & Year(Date), .Range("A6:BZ6"), 0)).FormulaArray = "=INDEX('" & Root & sourceSheet & ws.Name & " " & "SUM" & " " & monthNumber & "." & lastDay & "." & Format(Now(), "yy") & "'!$D:$D,MATCH(""Dealer1"",'" & Root & sourceSheet & ws.Name & " " & "SUM" & " " & monthNumber & "." & lastDay & "." & Format(Now(), "yy") & "'!$A:$A&'" & Root & sourceSheet & ws.Name & " " & "SUM" & " " & monthNumber & "." & lastDay & "." & Format(Now(), "yy") & "'!$B:$B,0))"
The .Replace setup I'm using is:
Dim theFormulaPart1 As String
Dim theFormulaPart2 As String
theFormulaPart1 = "=INDEX('" & Root & sourceSheet & ws.Name & " " & "SUM" & " " & monthNumber & "." & lastDay & "." & Format(Now(), "yy") & "'!$D:$D,MATCH(""Dealer1"",'" & Root & sourceSheet & ws.Name & " " & "X_X_X())"
theFormulaPart2 = "&" & "SUM" & " " & monthNumber & "." & lastDay & "." & Format(Now(), "yy") & "'!$A:$A&'" & Root & sourceSheet & ws.Name & " " & "SUM" & " " & monthNumber & "." & lastDay & "." & Format(Now(), "yy") & "'!$B:$B,0))"
.Cells(Application.WorksheetFunction.Match("Red Car", .Range("A:A"), 0), Application.WorksheetFunction.Match(ComboBox1.Value & " " & Year(Date), .Range("A6:BZ6"), 0)).FormulaArray = theFormulaPart1
.Cells(Application.WorksheetFunction.Match("Red Car", .Range("A:A"), 0), Application.WorksheetFunction.Match(ComboBox1.Value & " " & Year(Date), .Range("A6:BZ6"), 0)).Reaplce "X_X_X())", theFormulaPart2