The following stored procedure is not working, I have tried everything without success. Help please.
Sub Create_View()
Dim conn As ADODB.Connection
Set conn = Application.CurrentProject.Connection
conn.Execute "CREATE VIEW vw_RobPS AS " & _
"SELECT sid, " & _
"Lname, " & _
"Fname, " & _
"EmpStatus, " & _
"[Peax Update as of 6-18-15], " & _
"[Peax Update Date] " & _
"FROM Master_06-18-2015" & _
"WHERE [Peax Update Date] as PeaxUdDt is not null; "
Application.RefreshDatabaseWindow
ExitHere:
If Not conn Is Nothing Then
If conn.State = adStateOpen Then conn.Close
End If
Set conn = Nothing
End Sub
When, I run it, I get this error message:
Run-time error'-2147217900(80040e14)': Syntax error in FROM clause.
I can't find the problem.
Your table name includes dashes; enclose that name in square brackets
Include a space between the table name and WHERE
Don't try to alias a field in the WHERE clause
"FROM [Master_06-18-2015] " & _
"WHERE [Peax Update Date] is not null; "
Related
Below is my code. Can anyone help?
SQL1 runs well but SQL2 Gives me a run time error 3075 (missing operator) in query expression '*StockNo'.
Private Sub DataUpdate()
Dim SQL1 As String
Dim SQL2 As String
SQL1 = "UPDATE tblChemicalStock " & _
"INNER JOIN tblChemicalStock1 " & _
"ON(tblChemicalStock.ChemicalName = tblChemicalStock1.ChemicalName) " & _
"SET tblChemicalStock.Stock1 = tblChemicalStock1.Stock1, " & _
"tblChemicalStock.Stock2 = tblChemicalStock1.Stock2, " & _
"tblChemicalStock.TotalStock = tblChemicalStock1.TotalStock;"
DoCmd.RunSQL SQL1
SQL2 = "INSERT INTO tblChemicalStock ([StockNo],[ChemicalName],[Stock1],
[Stock2],[TotalStock],[CategoryCode])" & _
"SELECT *StockNo,ChemicalName,Stock1,Stock2,TotalStock,CategoryCode FROM tblChemicalStock1 t" & _
"WHERE NOT EXISTS(SELECT 1 FROM tblChemicalStock s" & _
"WHERE t.StockNo = s.StockNo);"
DoCmd.RunSQL SQL2
End Sub
In an event of a Form I write some code to select value from a table and insert it into another table. This is my code:
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT std_crs_absence.std_name, std_crs_absence.stg_number, std_crs_absence.crs_name, Sum(std_crs_absence.absence_time) AS SumOfabsence_time, Sum(std_crs_absence.molat) AS SumOfmolat " & _
"FROM std_crs_absence GROUP BY std_crs_absence.std_name, std_crs_absence.stg_number, std_crs_absence.crs_name ", dbOpenDynaset)
rs.MoveFirst
Do Until rs.EOF
sqlinsert = "INSERT INTO abs_summary ([std_name],[stg_number],[crs_name],[SumOfabsence_time],[SumOfmolat])" & _
" VALUES ('" & rs("std_name") & "','" & rs("stg_number") & "','" & rs("crs_name") & "'," & rs("absence_time") & "," & rs("molat") & ")"
DoCmd.RunSQL (sqlinsert)
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
When the event is executed it gives me an error that says "Item not found in this collection". What am I doing wrong?
When you first SELECT the data you retrieve the sums as SumOfabsence_time and SumOfmolat, but for the INSERT you try to use rs("absence_time") and rs("molat"). Those columns don't exist in the Recordset so you get the error. You'll need to use rs("SumOfabsence_time") and rs("SumOfmolat") instead.
(Obligatory comment: You really should be using a parameterized query instead of dynamic SQL.)
I always get this 2 errors when executing a query
A first chance exception of type 'System.IndexOutOfRangeException' occurred in MySql.Data.dll
Followed by
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
This is how I execute my query
Sub Get_Teller_Num(hardware_Add As String)
Check_DB_Con() 'Check if the connection is okay
SQL_Query = "SELECT teller_info.teller_num, service_info.service_num " _
& "FROM service_info " _
& "JOIN teller_info " _
& "ON service_info.service_id = teller_info.teller_id " _
& "WHERE teller_info.hardware_add = " & hardware_Add
Dim MySQL_CMD As New MySqlCommand(SQL_Query, MysqlConn)
Try
MySQL_CMD.Connection.Open()
MySQL_Reader = MySQL_CMD.ExecuteReader()
While MySQL_Reader.Read
teller_Num = MySQL_Reader("teller_info.teller_num")
service_Num = MySQL_Reader("service_info.service_num")
End While
MySQL_Reader.Close()
Catch myerror As MySqlException
Console.WriteLine("Failed to run query: " & myerror.Message)
Finally
MysqlConn.Close()
MysqlConn.Dispose()
End Try
End Sub
As you can see, I am joining another table so I can get certain values to it.
It seems that by adding ALIAS or AS in my WHERE clause and reading it based on it solves the problem.
So instead of this query:
SQL_Query = "SELECT teller_info.teller_num, service_info.service_num " _
& "FROM service_info " _
& "JOIN teller_info " _
& "ON service_info.service_id = teller_info.teller_id " _
& "WHERE teller_info.hardware_add = " & hardware_Add
Change it to (Notice that I create an ALIAS for the fields that I want to get):
SQL_Query = "SELECT teller_info.teller_num AS Teller_Num, service_info.service_num AS Service_Num " _
& "FROM service_info " _
& "JOIN teller_info " _
& "ON service_info.service_id = teller_info.teller_id " _
& "WHERE teller_info.hardware_add = " & hardware_Add
Then, we also need to change the way we read data.
Instead of this:
While MySQL_Reader.Read
teller_Num = MySQL_Reader("teller_info.teller_num")
service_Num = MySQL_Reader("service_info.service_num")
End While
Change it to (Notice that I read them based on the ALIAS that I've given to them):
While MySQL_Reader.Read
teller_Num = MySQL_Reader("Teller_Num")
service_Num = MySQL_Reader("Service_Num")
End While
To give a brief summary of the objective of this macro, I am trying to get the sum of cash between certain dates based on a set of parameters. I keep getting an error in VBA when I try to run a SQL query with a date. I'm not sure what the issue is but I think it's related to how I have the date formatted. I have tried running it multiple ways but keep getting a syntax error, whether it be related to 'a', '#', or 'table'.
Here's the query I'm using. Any help would be greatly appreciated.
Sub GetCashBalance()
Dim cn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim SQL As String
Dim StartDate As String
Dim EndDate As String
StartDate = InputBox("Enter Start Date in mm/dd/yy format.")
EndDate = InputBox("Enter End Date in mm/dd/yy format.")
SQL = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=master;Data Source=SERVER\ODS"
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.Open SQL
Set rs = cn.Execute("SELECT SUM(a.CASH)" & _
"FROM CUSTOMER_DATA.dbo.TRANSACTION_HISTORY a" & _
"LEFT JOIN CUSTOMER_DATA.dbo.DAILY_TRANSACTION b" & _
"ON a.T01_KEY = b.T01_KEY" & _
"WHERE PROC_DATE BETWEEN #StartDate# AND #EndDate#" & _
"AND a.CODE NOT IN ('22','23','M','2-L','36-R')" & _
"AND isnull(a.DESCRIPTION, '') NOT IN ('01','02','03','0DO1','0NF2');")
If Not rs.EOF Then
Sheets(7).Range("A40").CopyFromRecordset rs
rs.Close
Else
MsgBox "Error", vbCritical
End If
cn.Close
End Sub
Set rs = cn.Execute("SELECT SUM(a.CASH)" & _
"FROM CUSTOMER_DATA.dbo.TRANSACTION_HISTORY a" & _
"LEFT JOIN CUSTOMER_DATA.dbo.DAILY_TRANSACTION b" & _
"ON a.T01_KEY = b.T01_KEY" & _
"WHERE PROC_DATE BETWEEN '" & StartDate & "' AND '" & EndDate & _
"# AND a.CODE NOT IN ('22','23','M','2-L','36-R')" & _
"AND isnull(a.DESCRIPTION, '') NOT IN ('01','02','03','0DO1','0NF2');")
Take a look at below piece, that should be changed
BETWEEN '" & StartDate & "' AND '" & EndDate & _
"' AND ....
I think it is better to use ADODB.Command and Parameters instead of doing a concatenation in the sql query.
Hi guys I dont know if this makes sense but how can I query another query in VBA?
I will show with example below
This is my first query
strSQL1 = "SELECT DISTINCT SourceBank" _
& ", Fullname, FirstNames" _
& ", Surname, Company" _
& ", EmailAddress" _
& " FROM question" _
& " WHERE FirstNames = '" & strFirstNames & "'" _
Set rs = dbs.OpenRecordset(strSQL)
Then I want to do something like this. Query the first query
strSQL2 = "S"SELECT * from " & strSQL1
Set rs1 = dbs.OpenRecordset(strSQL)
I just want to know if this is possible and if not then what is the best way around it?
All I want to do is to be able to query another query/string/recordset.
thanks
You can do it almost like you've wrote:
strSQL2="SELECT * FROM (" & strSQL1 & ")"
but be sure not to include ; in strSQL1
upd, try:
strSQL2 = "SELECT Question.EmailAddress, SUBQUERY.EmailAddress &" _
& "FROM Question LEFT JOIN (" & strSQL1 & ") AS SUBQUERY ON Question.EmailAddress = SUBQUERY.EmailAddress"
OR just save sql1 into QueryDef (Query in ms access) and use it like source table.