How to insert into a TEMP Table in VBA - vba

When I run the following code I receive an error, "Run-time error '1004': Application-defined or object-defined error"
When I select debug, the following line is highlighted: .Refresh BackgroundQuery:=False
querystr = "SET NOCOUNT ON" & Chr(13) & _
"SELECT CSD.StoreNo AS 'StoreNo',SUM(CSD.Amount) as totalSales " & Chr(13) & _
"INTO #SalesOfTheStores " & Chr(13) & _
"FROM Purchase.dbo.CashsheetDetail as CSD " & Chr(13) & _
"INNER JOIN Purchase.dbo.CashsheetHeader as CSH on CSH.TransferID = CSD.TransferID and CSH.StoreNo = CSD.StoreNo " & Chr(13) & _
"WHERE CSD.Comments = 'Total Gross Sales' AND CSH.DayendDate between '" & StartDate & "' And '" & EndDate & "' " & Chr(13) & _
"GROUP BY CSD.StoreNo; "
With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DRIVER=SQL Server;SERVER=" & Div & "DBL01\SR;UID=" & User & ";APP=Microsoft Office 2003;WSID=" & PC & ";DATABASE=Purchase;Trusted_Connection=Yes" _
, Destination:=Range("A1"))
.RefreshStyle = xlOverwriteCells
.CommandText = querystr
.Refresh BackgroundQuery:=False
End With

Your query string does not return data. Change your SQL to be a SELECT statement rather than a SELECT INTO (remove the INTO clause).

Related

Query with conditions based on cells

I am trying to write a VBA code that Queries some values.
My SQL query has two conditions in the WHERE statement.
The value in column B is within the query, so no issue about it.
The value in column A is a code which starts with numbers, but contains Letters and should be seen as a string as such to work '6F3S'; specifically: database.columnA='6F3S'.
My issue comes from the fact that I want to outsource the element 6F3S in cell D1 in sheet "Input_sheet", so that the user can change it with other codes if necessary.
This is my code so far:
Sub Query1()
Dim ValueCellD1 As String
ValueCellD1 = Worksheets("Input_Sheet").Range("D1").Value
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"FFFF;DSN=XXXXXXXXXXXX;", Destination:=Range("$A$7")).QueryTable
.CommandText = Array( _
"SELECT database.columnA, database.columnB, database.columnC" _
& Chr(13) & "" & Chr(10) & _
"FROM IMPALA.database database" _
& Chr(13) & "" & Chr(10) & _
"WHERE (database.columnA=ValueCellD1) AND (database.columnB='London')")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = True
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "YYYYYYYYYYYYYYYYY"
.Refresh BackgroundQuery:=False
End With
End Sub
I receive a
Run-time error '13': Type mismatch
When I try to debug it appears being here:
.CommandText = Array( _
"SELECT database.columnA, database.columnB, database.columnC" _
& Chr(13) & "" & Chr(10) & _
"FROM IMPALA.database database" _
& Chr(13) & "" & Chr(10) & _
"WHERE (database.columnA=ValueCellD1) AND (database.columnB='London')")
Note: I am currently using Excel 2010
You need to get ValueCellD1 out of the string so it is recognized as variable.
.CommandText = Array( _
"SELECT database.columnA, database.columnB, database.columnC" _
& Chr(13) & "" & Chr(10) & _
"FROM IMPALA.database database" _
& Chr(13) & "" & Chr(10) & _
"WHERE (database.columnA='" & ValueCellD1 & "') AND (database.columnB='London')")
Actually I have no idea why you insert & Chr(13) & "" & Chr(10) & in your string. The following should work too:
.CommandText = Array( _
"SELECT database.columnA, database.columnB, database.columnC " _
"FROM IMPALA.database database " _
"WHERE (database.columnA='" & ValueCellD1 & "') AND (database.columnB='London')")
Note that if you read a cell value into your variable
ValueCellD1 = Worksheets("Input_Sheet").Range("D1").Value
and push this into your SQL command without verifying the cell value, then anyone who can edit the cell value in D1 can easily attack your database and run any SQL command he likes (eg delete it). Never trust user input. Always validate it.
See https://en.wikipedia.org/wiki/SQL_injection.

MS-Access union query syntax error within VBA code

I have made a SQL statement that calculates the sum of the chosen fields I've gathered. I have the queries working within MS Access, but when I translate it to VBA coding within my database it spits out a compile error 3319: Syntax error. Below I have attached my working query as well as my query with the syntax.
Query within MS Access is below that works properly:
SELECT 1,'Passed - Depot' AS QRY, Sum(IIf(([PreStressStackDate]>=[StartDate] And [PreStressStackDate]<=[EndDate]) And (([CurrentLevelOfCompletion]>=5 And [CurrentLevelOfCompletion]<1073741829) Or [CurrentLevelOfCompletion]>1073741829),1,0)) AS [PreStress Stackup], Sum(IIf(([StackCompressionDate]>=[StartDate] And [StackCompressionDate]<=[EndDate]) And (([CurrentLevelOfCompletion]>=21 And [CurrentLevelOfCompletion]<1073741845) Or [CurrentLevelOfCompletion]>1073741845),1,0)) AS [Stack Compression], Sum(IIf(([TestingDate]>=[StartDate] And [TestingDate]<=[EndDate]) And (([CurrentLevelOfCompletion]>85 And [CurrentLevelOfCompletion]<1073741909) Or [CurrentLevelOfCompletion]>1073741909),1,0)) AS Testing, Sum(IIf(([ShroudAssemblyDate]>=[StartDate] And [ShroudAssemblyDate]<=[EndDate]) And (([CurrentLevelOfCompletion]>=341 And [CurrentLevelOfCompletion]<1073742165) Or [CurrentLevelOfCompletion]>1073742165),1,0)) AS [Shroud Assembly], Sum(IIf(([TransformerInstallDate]>=[StartDate] And [TransformerInstallDate]<=[EndDate]) And (([CurrentLevelOfCompletion]>=1365 And [CurrentLevelOfCompletion]<1073743189)),1,0)) AS [Transformer Installation]
FROM TR343DrySide
WHERE (([TransducerSN] Not Like "CR*"));
UNION SELECT 2, 'Failed - Depot' AS QRY, Sum(IIf(([PreStressStackDate]>=[StartDate] And [PreStressStackDate]<=[EndDate]) And [CurrentLevelOfCompletion]=1073741829,1,0)) AS [PreStress Stackup], Sum(IIf(([StackCompressionDate]>=[StartDate] And [StackCompressionDate]<=[EndDate]) And [CurrentLevelOfCompletion]=1073741845,1,0)) AS [Stack Compression], Sum(IIf(([TestingDate]>=[StartDate] And [TestingDate]<=[EndDate]) And [CurrentLevelOfCompletion]=1073741909,1,0)) AS [Testing], Sum(IIf(([ShroudAssemblyDate]>=[StartDate] And [ShroudAssemblyDate]<=[EndDate]) And [CurrentLevelOfCompletion]=1073742165,1,0)) AS [Shroud Assembly], Sum(IIf(([TransformerInstallDate]>=[StartDate] And [TransformerInstallDate]<=[EndDate]) And [CurrentLevelOfCompletion]=1073743189,1,0)) AS [Transformer Installation]
FROM TR343DrySide
WHERE (([TransducerSN] Not Like "CR*"));
When this query is run, it tallies up the sum of the fields between the chosen dates.
Below I have attached my VBA code that comes up with a syntax compile error 3319:
Private Sub cmdDrySideRunReport_Click()
Dim strDrySQL_New, strDrySQL_Depot As String
Dim DryStartDate As Date
Dim DryEndDate As Date
'------------------------------------------------------------------------------------------------------
If IsNull(Me.txtDryStartDate) Or Me.txtDryStartDate = "" Or IsNull(Me.txtDryEndDate) Or Me.txtDryEndDate = "" Then
If IsNull(Me.txtDryStartDate) Or Me.txtDryStartDate = "" Then
MsgBox "Please enter the Start Date"
Me.txtDryStartDate.SetFocus
End If
If IsNull(Me.txtDryEndDate) Or Me.txtDryEndDate = "" Then
MsgBox "Please enter the End Date"
Me.txtDryEndDate.SetFocus
End If
Else
DryStartDate = Me.txtDryStartDate
DryEndDate = Me.txtDryEndDate + 1
'###########################################################
'DRYSIDE NEW
strDrySQL_New = "Select 1, 'Passed - New' AS QRY, Sum(IIf(([PreStressStackDate]>=#" & DryStartDate & "# And [PreStressStackDate]<=#" & DryEndDate & "#)" & _
" And (([CurrentLevelOfCompletion]>=5 And [CurrentLevelOfCompletion]<1073741829) Or [CurrentLevelOfCompletion]>1073741829),1,0)) AS [PreStress Stackup]," & _
" Sum(IIf(([StackCompressionDate]>=#" & DryStartDate & "# And [StackCompressionDate]<=#" & DryEndDate & "#) And (([CurrentLevelOfCompletion]>=21" & _
" And [CurrentLevelOfCompletion]<1073741845) Or [CurrentLevelOfCompletion]>1073741845),1,0)) AS [Stack Compression]," & _
" Sum(IIf(([TestingDate]>=#" & DryStartDate & "# And [TestingDate]<=#" & DryEndDate & "#) And (([CurrentLevelOfCompletion]>=85" & _
vbCrLf & " And [CurrentLevelOfCompletion]<1073741909) Or [CurrentLevelOfCompletion]>1073741909),1,0)) AS [Testing]," & _
" Sum(IIf(([ShroudAssemblyDate]>=#" & DryStartDate & "# And [ShroudAssemblyDate]<=#" & DryEndDate & "#) And (([CurrentLevelOfCompletion]>=341" & _
" And [CurrentLevelOfCompletion]<1073742165) Or [CurrentLevelOfCompletion]>1073742165),1,0)) AS [Shroud Assembly]," & _
" Sum(IIf(([TransformerInstallDate]>=#" & DryStartDate & "# And [TransformerInstallDate]<=#" & DryEndDate & "#) And (([CurrentLevelOfCompletion]>=1365 And [CurrentLevelOfCompletion]<1073743189)),1,0)) AS [Transformer Installation]" & _
" FROM TR343DrySide" & _
" WHERE (([TransducerSN] Like ""CR*""))" & _
vbCrLf & " UNION SELECT 2, 'Failed - New' AS QRY, Sum(IIf(([PreStressStackDate]>=#" & DryStartDate & "# And [PreStressStackDate]<=#" & DryEndDate & "#)" & _
" And [CurrentLevelOfCompletion]=1073741829),1,0)) AS [PreStress Stackup], Sum(IIf(([StackCompressionDate]>=#" & DryStartDate & "#" & _
" And [StackCompressionDate]<=#" & DryEndDate & "#) And [CurrentLevelOfCompletion]=1073741845,1,0)) AS [Stack Compression]," & _
" Sum(IIf(([TestingDate]>=#" & DryStartDate & "# And [TestingDate]<=#" & DryEndDate & "#) And [CurrentLevelOfCompletion]=1073741909,1,0)) AS [Testing]," & _
" Sum(IIf(([ShroudAssemblyDate]>=#" & DryStartDate & "# And [ShroudAssemblyDate]<=#" & DryEndDate & "#) And ([CurrentLevelOfCompletion]=1073742165 Or" & _
" [CurrentLevelOfCompletion]=1073742165),1,0)) AS [Shroud Assembly], Sum(IIf(([TransformerInstallDate]>=#" & DryStartDate & "# And [TransformerInstallDate]<=#" & DryEndDate & "#)" & _
" And [CurrentLevelOfCompletion]=1073743189,1,0)) AS [Transformer Installation]" & _
" FROM TR343Dryside" & _
" WHERE (([TransducerSN] Like ""CR*""));"
Me.sfrmCraneDrySidePassFailDateRange_New.Form.RecordSource = strDrySQL_New
Me.sfrmCraneDrySidePassFailDateRange_New.Visible = True
End If
End Sub
The result when the query is activated is: Run-time error '3319':
Syntax error within union query.
The problem is raised within the line of:
Me.sfrmCraneDrySidePassFailDateRange_New.Form.RecordSource = strDrySQL_New
At least here, you have an extra closing parenthesis:
.. And [CurrentLevelOfCompletion]=1073741829),1,0))
Should be:
.. And [CurrentLevelOfCompletion]=1073741829,1,0))
Insert a line:
Debug.Print strDrySQL_New
and study the print.

SQL NOOB - nested/subqueires

I will begin by clearly stating that I am not a programmer, I am an accountant at heart!
I have a need to return into excel all transactions relating to jobs that have had transactions in a given week (i.e. so that I can see an in week amount and total to date amount).
I'm reasonably proficient with VBA in excel (as accountants go anyway!) but I have always just copied and bodged the same old SQL statement. Essentially, what I think I need to do is a sub query in place of the order number of the WHERE statement in the following:
With Sheet1.QueryTables.Add(Connection:=Array(Array( _
"ODBC;DRIVER={iSeries Access ODBC Driver};SYSTEM=JADE;DBQ=QGPL LIVDTALIB;DFTPKGLIB=QGPL;LANGUAGEID=ENU;PKG=QGPL/DEFAULT(IBM),2,0,1,0,"), _
Array("512;QRYSTGLMT=-1;")), Destination:=Sheet1.Range("A1"))
.CommandText = Array( _
"SELECT SLBGDTPF.BGMCU, SLBGDTPF.BGDSDT, SLBGDTPF.ORTYPE, SLBGDTPF.ORDNO, SLBGDTPF.BGDSVL, SLBGDTPF.BGCD, ", _
"SLBGDTPF.ADBBG, SLBGDTPF.BGRMK" _
& Chr(13) & "" & Chr(10) & _
"FROM RCHASE5C.LIVDTALIB.SLBGDTPF SLBGDTPF" _
& Chr(13) & "" & Chr(10) & _
"WHERE (SLBGDTPF.ORDNO='30214884')")
.Name = "TEST Query"
.FieldNames = True
.RefreshStyle = xlOverwriteCells
.Refresh BackgroundQuery:=False
End With
As a standalone query, what the sub-query element looks like is as follows:
With Sheet2.QueryTables.Add(Connection:=Array(Array( _
"ODBC;DRIVER={iSeries Access ODBC Driver};SYSTEM=JADE;DBQ=QGPL LIVDTALIB;DFTPKGLIB=QGPL;LANGUAGEID=ENU;PKG=QGPL/DEFAULT(IBM),2,0,1,0,"), _
Array("512;QRYSTGLMT=-1;")), Destination:=Sheet2.Range("A1"))
.CommandText = Array( _
"SELECT SLBGDTPF.ORDNO" _
& Chr(13) & "" & Chr(10) & _
"FROM RCHASE5C.LIVDTALIB.SLBGDTPF SLBGDTPF" _
& Chr(13) & "" & Chr(10) & _
"WHERE SLBGDTPF.BGPSDT='20180420'" _
& Chr(13) & "" & Chr(10) & _
"GROUP BY SLBGDTPF.ORDNO")
.Name = "TEST Query"
.FieldNames = True
.RefreshStyle = xlOverwriteCells
.Refresh BackgroundQuery:=False
End With
I'm open to all suggestions, including alternate approaches (I did try using IN and referencing a range in a sheet but I need to pass up to 1,000 different job numbers)
Just as an FYI, this is a template that will be sent out to people within the business to update themselves hence the need to build the connection, rather than just have them refresh an existing query(s) within the workbook.
All the best,
Joe
This can be achieved a few ways, but presuming the database can handle subqueries, I would try a WHERE IN term. I've also made some other cursory edits for clarity. The ultimate GROUP BY term is redundant in the subquery as there is only a single subquery SELECT field and no aggregation going on.
With Sheet1.QueryTables.Add(Connection:=Array(Array( _
"ODBC;DRIVER={iSeries Access ODBC Driver};SYSTEM=JADE;DBQ=QGPL LIVDTALIB;DFTPKGLIB=QGPL;LANGUAGEID=ENU;PKG=QGPL/DEFAULT(IBM),2,0,1,0,"), _
Array("512;QRYSTGLMT=-1;")), Destination:=Sheet1.Range("A1"))
.CommandText = Array( _
"SELECT SLBGDTPF.BGMCU, SLBGDTPF.BGDSDT, SLBGDTPF.ORTYPE, SLBGDTPF.ORDNO, SLBGDTPF.BGDSVL, SLBGDTPF.BGCD, SLBGDTPF.ADBBG, SLBGDTPF.BGRMK" & vbCrLf & _
"FROM RCHASE5C.LIVDTALIB.SLBGDTPF SLBGDTPF" & vbCrLf & _
"WHERE SLBGDTPF.ORDNO IN (" & vbCrLf & _
"SELECT SLBGDTPF.ORDNO" & vbCrLf & _
"FROM RCHASE5C.LIVDTALIB.SLBGDTPF SLBGDTPF" & vbCrLf & _
"WHERE SLBGDTPF.BGPSDT='20180420')")
.Name = "TEST Query"
.FieldNames = True
.RefreshStyle = xlOverwriteCells
.Refresh BackgroundQuery:=False
End With
As #IanPeters already mentioned, there are a few ways to handle this in SQL. I would like to add two versions that use a join instead of one of the WHERE clauses.
You might want to test which version performs best on your database. This will depend on the index structure on the database and on how well the query optimizer handles the query.
Subquery in join:
With Sheet1.QueryTables.Add(Connection:=Array(Array( _
"ODBC;DRIVER={iSeries Access ODBC Driver};SYSTEM=JADE;DBQ=QGPL LIVDTALIB;DFTPKGLIB=QGPL;LANGUAGEID=ENU;PKG=QGPL/DEFAULT(IBM),2,0,1,0,"), _
Array("512;QRYSTGLMT=-1;")), Destination:=Sheet1.Range("A1"))
.CommandText = Array( _
"SELECT SLBGDTPF.BGMCU, SLBGDTPF.BGDSDT, SLBGDTPF.ORTYPE, SLBGDTPF.ORDNO, SLBGDTPF.BGDSVL, SLBGDTPF.BGCD, SLBGDTPF.ADBBG, SLBGDTPF.BGRMK" & vbCrLf & _
"FROM RCHASE5C.LIVDTALIB.SLBGDTPF SLBGDTPF" & vbCrLf & _
"INNER JOIN" & vbCrLf & _
"(SELECT S.ORDNO" & vbCrLf & _
"FROM RCHASE5C.LIVDTALIB.SLBGDTPF S" & vbCrLf & _
"WHERE WeekOrders.BGPSDT='20180420') WeekOrders" & vbCrLf & _
"ON SLBGDTPF.ORDNO = WeekOrders.ORDNO")
.Name = "TEST Query"
.FieldNames = True
.RefreshStyle = xlOverwriteCells
.Refresh BackgroundQuery:=False
End With
Condition outside of join:
With Sheet1.QueryTables.Add(Connection:=Array(Array( _
"ODBC;DRIVER={iSeries Access ODBC Driver};SYSTEM=JADE;DBQ=QGPL LIVDTALIB;DFTPKGLIB=QGPL;LANGUAGEID=ENU;PKG=QGPL/DEFAULT(IBM),2,0,1,0,"), _
Array("512;QRYSTGLMT=-1;")), Destination:=Sheet1.Range("A1"))
.CommandText = Array( _
"SELECT SLBGDTPF.BGMCU, SLBGDTPF.BGDSDT, SLBGDTPF.ORTYPE, SLBGDTPF.ORDNO, SLBGDTPF.BGDSVL, SLBGDTPF.BGCD, SLBGDTPF.ADBBG, SLBGDTPF.BGRMK" & vbCrLf & _
"FROM RCHASE5C.LIVDTALIB.SLBGDTPF SLBGDTPF" & vbCrLf & _
"INNER JOIN RCHASE5C.LIVDTALIB.SLBGDTPF WeekOrders" & vbCrLf & _
"ON SLBGDTPF.ORDNO = WeekOrders.ORDNO" & vbCrLf & _
"WHERE WeekOrders.BGPSDT='20180420'")
.Name = "TEST Query"
.FieldNames = True
.RefreshStyle = xlOverwriteCells
.Refresh BackgroundQuery:=False
End With

Runtime error '1004' General odbc error while refreshing excel sheet to get updated data

We are using shared folder in server where we keep all excel sheets based on our business requirement so whoever requires that document he will picked up that document from that shared folder and he will receive all update/manipulated data by clicking on "Refresh" button in "Data" tab in excel 2007,so in my organization everybody pc is working fine and they are getting updated data by refreshing document but in my pc the movement i click on refresh i am getting this error which is in image below please provide me a clear answer.
Sub TT_Out()
' ' Macro2 Macro
Dim RngFromDate, RngToDate
RngFromDate = InputBox("Enter Start Date !", "TT Out", Date - 1)
RngToDate = InputBox("Enter End Date !", "TT Out", RngFromDate)
With Range("Table_Query_from_ALXORCL[TT_OUT_DATE]").ListObject.QueryTable
.Connection = Array(Array( _
"ODBC;DRIVER={Oracle in instantclient_12_1};" & _
"SERVER=ALXORCL;UID=ALXLIVE;PWD=alx123;" & _
"DBQ=ALXORCL;DBA=W;APA=T;EXC=F;XSM=Default;FEN=T;QTO=T;FRC=10;F"), _
Array("DL=10;LOB=T;RST=T;BTD=F;BNF=F;BAM=IfAllSuccessful;NUM=NLS;" & _
"DPM=F;MTS=T;MDI=Me;CSR=F;FWC=F;FBS=60000;TLO=O;" & _
"MLD=0;ODA=F;STE=F;TSZ=8"), Array("192;"))
.CommandText = Array( _
" SELECT ALX_TT_OUT.TT_OUT_CODE, " & _
" ALX_TT_OUT.TT_OUT_DATE, " & _
" ALX_TT_OUT.F_NAME, " & _
" ALX_TT_OUT.B_F_NAME, " & _
" ALX_TT_OUT.SENDING_PRPS, " & _
" ALX_LOOKUP_DET.LOOKUP_DET_NAME||'-'||ALX_TT_OUT.DOC_NO, " & _
" ALX_PRODUCT.PRODUCT_CODE, " & _
" ALX_TT_OUT.QTY*ALX_TT_OUT.SELL_RATE, " & _
" ALX_CORRESPONDENT.CORRESPONDENT_NAME" & Chr(13) & Chr(10) & _
" FROM ALXTEST.ALX_CORRESPONDENT ALX_CORRESPONDENT, ALXTEST2.ALX_LOOKUP_DET ALX_LOOKUP_DET, ALXTEST2.ALX_PRODUCT ALX_PRODUCT, ALXL", _
" IVE.ALX_TT_OUT ALX_TT_OUT" & Chr(13) & "" & Chr(10) & _
" WHERE ALX_PRODUCT.PRODUCT_ID = ALX_TT_OUT.PRODUCT_ID " & _
" AND ALX_TT_OUT.CORRESPONDENT_ID = ALX_CORRESPONDENT.CORRESPONDENT_ID " & _
" AND ALX_LOOKUP_DET.LOOKUP_DET_ID = ALX_TT_OUT.DOC_TYPE_L ", _
" AND ((ALX_TT_OUT.TT_OUT_CODE Not Like '%HOF%') " & _
" AND (to_date(TT_OUT_DATE) Between '" & RngFromDate & "' And '" & RngToDate & "') " & _
" )")
.Refresh BackgroundQuery:=False
End With
End Sub

Microsoft Access/Updating Record

Can anyone help me with the below code to edit the records please?Im trying to edit but there is a Syntax error and I cant fix that.
See the error that appears below.
Run Time Error '3075'
Syntax Error(missing operator) in query expresion 'TypeID='.
Thanks
Private Sub oshaadd_Click()
'On Error Resume Next
If (IsNull(Me.oshaID) Or (Me.oshaID = "") And IsNull(Me.oshatype) Or (Me.oshatype = "")) Then
MsgBox "please fill required fields!", vbInformation, "Information"
Exit Sub
End If
If Me.oshaID.Tag & "" = "" Then
CurrentDb.Execute "INSERT INTO osha(TypeID, OSHA)" & _
"VALUES ('" & Me.oshaID & "', '" & Me.oshatype & "')"
If MsgBox("Added", vbOKOnly) Then
Me.osha_subform.Form.Requery
End If
Else
CurrentDb.Execute "UPDATE osha " & _
"SET TypeID =" & Me.oshaID & _
", OSHA = '" & Me.oshatype & "'" & _
"WHERE TypeID =" & Me.oshatype.Tag
MsgBox "Updated", vbInformation, "Information"
Me.oshaadd.Caption = "Add"
Me.oshaedit.Enabled = True
End If
Me.osha_subform.Form.Requery
End Sub
Private Sub oshaedit_Click()
On Error Resume Next
If Not (Me.osha_subform.Form.Recordset.EOF And Me.osha_subform.Form.Recordset.BOF) Then
With Me.osha_subform.Form.Recordset
Me.oshaID = .Fields("TypeID")
Me.oshatype = .Fields("OSHA")
Me.oshaID.Tag = .Fields("TypeID")
Me.oshaadd.Caption = "Update"
Me.oshaedit.Enabled = False
End With
End If
End Sub
You forget to put the database single quote around Me.oshaID and Me.oshatype.Tag in the UPDATE SQL statement:
CurrentDb.Execute "UPDATE osha " & _
"SET TypeID ='" & Me.oshaID & "'" & _
", OSHA = '" & Me.oshatype & "'" & _
" WHERE TypeID ='" & Me.oshatype.Tag &"';"
CurrentDb.Execute "UPDATE osha " & _
"SET TypeID =" & Me.oshaID & _
", OSHA = '" & Me.oshatype & "'" & _
"WHERE TypeID =" & Me.oshatype.Tag
MsgBox "Updated", vbInformation, "Information"
Me.oshaadd.Caption = "Add"
Me.oshaedit.Enabled = True
In this block of your code where you have "SET TypeID =" and "WHERE TypeID =" try adding a space after the equals sign so it reads "TypeID = ". If that doesn't resolve the error try adding single quotes around the value you are assigning to TypeID (TypeID = '" & Me.oshaID & _"',)