Select max visual studio 2010 - sql

I'm updating a program from Visual Basic 6 to Visual Studio 2010 and, of course, I have founded a lot of problems so solve.
I'm using Access database with four tables with the same Key (Indice).
If I use the code as follow, I can get the last record from CodDekafix Table:
Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLast.Click
Dim Con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\DEKAFIX\Consulta Dekafix\dekafix.mdb")
Dim cmd As New OleDbCommand()
Con.Open()
sql = "Select * From Indice Where CodDekafix=(Select max(CodDekafix) From Indice)"
But If I want to get all the results from all tables with the same Key (Indice) with the change as showed below the program doesn't work.
sql = "Select * from Indice, dekafix1, dekafix2, dekafix3" _
& " where CodDekafix=(Select max(CodDekafix) From Indice) and" _
& " Indice.CodDekafix = dekafix1.CodDekafix and" _
& " dekafix1.CodDekafix=dekafix2.CodDekafix and" _
& " dekafix2.CodDekafix=dekafix3.CodDekafix and" _
& " ORDER BY Indice.CodDekafix"

Your SQL code in the second code sample is invalid. When we strip out the formatting representing the string in VB code, you get this:
Select * from Indice, dekafix1, dekafix2, dekafix3
where CodDekafix=(Select max(CodDekafix) From Indice) and
Indice.CodDekafix = dekafix1.CodDekafix and
dekafix1.CodDekafix=dekafix2.CodDekafix and
dekafix2.CodDekafix=dekafix3.CodDekafix and
ORDER BY Indice.CodDekafix
You have two problems here:
1) There's an extra "and" before the ORDER BY clause. Remove it.
2) The first line of your WHERE clause has an ambiguous reference to
CodDekafix -- you need to specify what table that's coming from.
Replacing CodDekafix with Indice.CodDekafixshould do the trick.
sql = "Select * from Indice, dekafix1, dekafix2, dekafix3" _
& " where Indice.CodDekafix=(Select max(CodDekafix) From Indice) and" _
& " Indice.CodDekafix = dekafix1.CodDekafix and" _
& " dekafix1.CodDekafix=dekafix2.CodDekafix and" _
& " dekafix2.CodDekafix=dekafix3.CodDekafix" _
& " ORDER BY Indice.CodDekafix"

Related

SQL query works in access but not in excel

I need to get data from a recordset. The SQL query works fine in MS Access and returns exactly the expected values, but when the same query is lunched in VBA Excel, I get the following error:
No value given for one or more required parameters
Do you have any ideas why this problem occurs?
Thank you.
Philippe-Olivier Roussel
Private Sub CBtype_AfterUpdate()
Dim strConnexion As String
Dim connexion As New ADODB.Connection
strConnexion = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & Database & ""
connexion.Open strConnexion
Dim rsMarque As New ADODB.Recordset
Dim seltype As String
seltype = CBtype.Value
rsMarque.Open "SELECT DISTINCT tblMarque.marque_nom FROM tblMarque, tblModele WHERE " & _
" tblMarque.marque_id = tblModele.marque_id AND tblModele.marque_id IN " & _
" (SELECT DISTINCT tblModele.marque_id FROM tblModele, tblType " & _
" WHERE tblModele.type_id = tblType.type_id AND tblModele.type_id = " & _
" (SELECT tblType.type_id FROM tblType WHERE " & _
" (tblType.type_nom = " & seltype & ")))", connexion, adOpenStatic
rsMarque.MoveFirst
With UserForm2.CBmarque
.Clear
Do
.AddItem rsMarque!marque_nom
rsMarque.MoveNext
Loop Until rsMarque.EOF
End With
End Sub
This error message looks like an output from the DBMS rather than Excel
I think you might be missing the apostrophe before and after your string variable. See if
" (tblType.type_nom = '" & seltype & "')))" works (I'm assuming the column you're querying is varchar type, since you declared seltype as string)

Reference userform data in excel vba sql statements?

I'm trying to reference userform data in SQL queries in excel 2010. When running the following code as my form submit action, I get an error that one or more parameters have not been provided in the SQL query.
Private Sub SubmitButton_Click()
Dim Connection As ADODB.Connection
Dim ConnectionString As String
ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source=I:\DB.xlsx; Extended Properties=""Excel 12.0 Xml; HDR=YES"";"
Dim SQL As String
'Transfer information
SQL = "INSERT INTO [Tests$]" & _
"(PartNo, RootCause, CorrectiveAction, AssignedTo, Verification, Comments, DateOpened, DateClosed, " & _
"Status, Purpose, ProdDate, ShipDate, TestDate, SampleDate, CusName, Facility, Result, Wire, StartingDia, " & _
"WireType, IncomingDia, FailMode, RootCauseDetail)" & _
"VALUES" & _
"([Forms][frmTesting]![PartNo], [Forms][frmTesting]![RCause], [Forms][frmTesting]![Corrective], " & _
"[Forms][frmTesting]![Assigned], [Forms][frmTesting]![Verification], [Forms][frmTesting]![Comments], " & _
"[Forms][frmTesting]![OpenDate], [Forms][frmTesting]![CloseDate], [Forms][frmTesting]![Status], " & _
"[Forms][frmTesting]![Purpose], [Forms][frmTesting]![ProdDate], [Forms][frmTesting]![ShipDate], " & _
"[Forms][frmTesting]![TestDate], [Forms][frmTesting]![SampleDate], [Forms][frmTesting]![Customer], [Forms][frmTesting]![Facility], " & _
"[Forms][frmTesting]![Result], [Forms][frmTesting]![Wire], [Forms][frmTesting]![StartingDia], " & _
"[Forms][frmTesting]![WireType], [Forms][frmTesting]![IncomingDia], [Forms][frmTesting]![FailMode], [Forms][frmTesting]![RCDetail])"
Set Connection = New ADODB.Connection
Call Connection.Open(ConnectionString)
Call Connection.Execute(SQL, , CommandTypeEnum.adCmdText Or ExecuteOptionEnum.adExecuteNoRecords)
Connection.Close
Set Connection = Nothing
End Sub
Am I using the correct form object reference syntax for SQL queries? Is there a difference between Excel and Access with how to reference form data?
In case any viewers run across the same issue and are interested. I found the easiest solution to be something like SQL = "INSERT INTO [Tests$] (PartNo) Values ('" & frmTesting.PartNo & "')"

SQL Program UPDATE Record Error

I'm working in a small SQL Database program. The programs just there to view, edit and update the database records. Everything is working remarkably well considering I've never tried something like this before. I've managed to get the Add Records, Refresh Records and Delete Records functions working flawlessly. However, I've hit a little bump when trying to UPDATE a selected record.
To clarify, the SQL Table is displayed in a list view, from this list view the end-user can select a particular-record and either edit or delete it.
The edit button opens a new form window with text fields which are automatically filled with the current information of that record.
The code for the edit record form is:
Private Sub frmEdit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
intDB_ID_Selected = CInt(frmMain.lvRec.SelectedItems(0).Text)
Call dispCaption()
Call dispInfo() 'Display the info of the selected ID
End Sub
Private Sub dispInfo()
SQL = "Select * from PersonsA " & _
"where Members_ID=" & intDB_ID_Selected & ""
With comDB
.CommandText = SQL
rdDB = .ExecuteReader
End With
If rdDB.HasRows = True Then
rdDB.Read()
Me.midtxt.Text = rdDB!Members_ID.ToString.Trim
Me.gttxt.Text = rdDB!Gamer_Tag.ToString.Trim
Me.sntxt.Text = rdDB!Screenname.ToString.Trim
Me.fntxt.Text = rdDB!First_Name.ToString.Trim
Me.lntxt.Text = rdDB!Last_Name.ToString.Trim
Me.dobtxt.Text = rdDB!DoB.ToString.Trim
Me.dobtxt.Text = rdDB!DoB.ToString.Trim
Me.emailtxt.Text = rdDB!E_Mail_Address.ToString.Trim
Me.teamptxt.Text = rdDB!Position.ToString.Trim
Me.ugctxt.Text = rdDB!Cautions.ToString.Trim
Me.recordtxt.Text = rdDB!Record.ToString.Trim
Me.eventatxt.Text = rdDB!Event_Attendance.ToString.Trim
Me.Mstattxt.Text = rdDB!Member_Status.ToString.Trim
End If
rdDB.Close()
End Sub
Private Sub dispCaption()
End Sub
Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
Call disControl()
'Validation
If invalidUpdateEntry() = True Then
Call enaControl()
Exit Sub
End If
'Prompt the user if the record will be updated
If MsgBox("Are you sure you want to update the selected record?", CType(MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2 + MsgBoxStyle.Question, MsgBoxStyle), "Update") = MsgBoxResult.Yes Then
'Update query
SQL = "Update PersonsA" & _
"SET Members_ID='" & Me.midtxt.Text.Trim & "'," & _
"Gamer_Tag='" & Me.gttxt.Text.Trim & "'," & _
"Screenname='" & Me.sntxt.Text.Trim & "'," & _
"First_Name='" & Me.fntxt.Text.Trim & "'," & _
"Last_Name='" & Me.lntxt.Text.Trim & "'," & _
"DoB='" & Me.dobtxt.Text.Trim & "'," & _
"E_Mail_Address='" & Me.emailtxt.Text.Trim & "'," & _
"Position='" & Me.teamptxt.Text.Trim & "'," & _
"U_G_Studio='" & Me.ugptxt.Text.Trim & "'," & _
"Cautions='" & Me.ugctxt.Text.Trim & "'," & _
"Record='" & Me.recordtxt.Text.Trim & "'," & _
"Event_Attendance='" & Me.eventatxt.Text.Trim & "'," & _
"Member_Status='" & Me.Mstattxt.Text.Trim & "'" & _
"WHERE Members_ID='" & intDB_ID_Selected & "'"
Call execComDB(SQL) 'Execute the query
Me.Close()
'*** Refresh the list
SQL = "Select * from PersonsA "
Call frmMain.dispRec(SQL)
'--- End of refreshing the list
Exit Sub
Else
Call enaControl()
End If
End Sub
As I've said, I've been able to do everything else using an extremely similar method, but when I try to UPDATE the record I get an error saying
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Incorrect syntax near 'Members_ID'.
I know it's this line that's the problem
"WHERE Members_ID='" & intDB_ID_Selected & "'"
Call execComDB(SQL) 'Execute the query
But referencing 'intDB_ID_Selected' has always worked before, and it's been set-up on the update form records load as intDB_ID_Selected = CInt(frmMain.lvRec.SelectedItems(0).Text)
I know this is a huge thread but if anyone could steer me in the right direction WITHOUT telling me to re-write the entire statement I'd be forever grateful.
EDIT1: I fixed the comma before the WHERE clause, but I'm still getting the same error.
Missing a space between
"Update PersonsA " & _
"SET Members_ID= ....
and (as already pointed out) a comma not needed before the WHERE
Said that, do a favor to yourself and to your users. Do not use string concatenation to build a sql command. Use always a parameterized query.
Just as an example
SQL = "Update PersonsA SET Members_ID=#id, Gamer_Tag=#tag, Screenname=#screen," & _
"First_Name=#fname,Last_Name=#lname,DoB=#dob,E_Mail_Address=#email," & _
"Position=#pos,U_G_Studio=#studio,Cautions=#caution,Record=#rec," & _
"Event_Attendance=#event, Member_Status=#stat " & _
"WHERE Members_ID=#id"
SqlCommand cmd = new SqlCommand(SQL, connection)
cmd.Parameters.AddWithValue("#id", Me.midtxt.Text.Trim)
..... so on for the other parameters defined above ....
cmd.ExecuteNonQuery();
Change "Member_Status='" & Me.Mstattxt.Text.Trim & "'," & _
to "Member_Status='" & Me.Mstattxt.Text.Trim & "'" & _
Looks like it was just an extra rogue comma!
On any error like this, use debugging provided with Visual Studio. Inspect the value of SQL, paste into MS SQL Management Studio - it has syntax highlight, and you should be able to spot the error easily.
To prevent further issues (including SQL injection vulnerability), separate this query into an embedded resource, and use parameters. Then it's easy to view, maintain (you can copy/paste between SQL Mgmt Studio and VS), and ultimately use it in code.
A side note, you don't need to use Call in VB.NET, just put a method name with parenthesis.

Indexed Property error

While debugging my code I found an inner exception which reads as:
In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.
In sql the script runs smoothly returning all the rows, but in vb it's not returning or finding any rows. I have checked that the primary key is in the table and defined. How can I fix this? I had a proir query which returned rows, but as soon as I added this new query the inner exception came.... I didn't change anything except the querystring.
This is what I've done:
Public Sub BindDeliveredItems()
Proir if statemets....
Else
queryString = "select distinct LS.[Route], LS.[SubRoute], LS.[Truck], Convert(VARCHAR(10), LS.[Date], 121) AS Date, LS.[DriverAssistantContract]" & _
",((del.CT*100 )/ todel.TCT) as Score" & _
"from [Warehouse].[dbo].[LoadSheet] LS" & _
"left join (select [Truck],[Date],[Status], count([Status]) CT from [Warehouse].[dbo].[LoadSheet]" & _
"WHERE status='Delivered'" & _
"group by [Truck],[Date],[Status]) Del" & _
"on LS.truck=Del.truck and LS.[Date]=del.[Date]" & _
"left join (select [Truck],[Date], count([Truck]) TCT from [Warehouse].[dbo].[LoadSheet]" & _
"group by [Truck],[Date]) todel" & _
"on LS.truck=toDel.truck and LS.[Date]=todel.[Date]" & _
"WHERE ls.[Date] = '2013-07-03'" & _
"AND ls.[Truck] = 'BX 39 LK GP'"
End If
Dim ds As DataSet = GetData(queryString)
If (ds.Tables.Count > 0) Then
gvDeliveredItems.DataSource = ds
gvDeliveredItems.PageSize = (10)
gvDeliveredItems.AllowPaging = True
gvDeliveredItems.DataBind()
End If
End Sub
Function GetData(ByVal queryString As String) As DataSet
Dim ds As New DataSet()
Try
Dim adapter As New SqlDataAdapter(queryString, SQLCon)
adapter.Fill(ds)
Catch ex As Exception
MessageBox(ex.Message)
End Try
Return ds
End Function
EDIT:
The first time the I can see the exception is on this line
Dim ds As DataSet = GetData(queryString)
And on this line the exception shows as well:
If (ds.Tables.Count > 0) Then
My previous sql string work perfect without changing anything. The only thing I changed was the querystring when it started to give me this exception
I'd suggest adding more whitespace into your query, e.g.:
queryString = "select distinct LS.[Route], LS.[SubRoute], LS.[Truck], Convert(VARCHAR(10), LS.[Date], 121) AS Date, LS.[DriverAssistantContract]" & vbCrLf & _
",((del.CT*100 )/ todel.TCT) as Score" & vbCrLf & _
"from [Warehouse].[dbo].[LoadSheet] LS" & vbCrLf & _
vbCrLf is a VB-ism. If you prefer, you could use Environment.NewLine
At the moment, there are no new lines or spaces inside the string from this string concatenation, so for example, those second and third lines join together as:
,((del.CT*100 )/ todel.TCT) as Scorefrom [Warehouse].[dbo].[LoadSheet] LS
Which isn't what you wanted, presumably.

VBA string length problem

I have an Access application where everytime a user enters the application, it makes a temp table for that user called 'their windows login name'_Temp. In one of my reports I need to query using that table, and I can't just make a query and set it as the recourdsource of the report, since the name of the table is always different.
What I tried then was to programatically set the recordset of the report by running the query and setting the form's recordset as the query's recordset. When I tried this, it kept giving me an error about the query.
I tried to debug, and I found that the string variable isn't able to contain the whole query at once. When I ran it with break points and added a watch for the string variable, it shows me that it cuts off the query somewhere in the middle.
I've experienced this problem before, but that was with an UPDATE query. Then, I just split it into two queries and ran both of them separately. This one is a SELECT query, and there's no way I can split it. Please help!
Thank you
Heres what I've tried doing:
ReturnUserName is a function in a module that returns just the login id of the user
Private Sub Report_Open(Cancel As Integer)
Dim strQuery As String
Dim user As String
user = ReturnUserName
strQuery = "SELECT " & user & "_Temp.EmpNumber, [FName] & ' ' & [LName] AS [Employee Name], " & _
"CourseName, DateCompleted, tblEmp_SuperAdmin.[Cost Centre] " & _
"FROM (tblCourse INNER JOIN (" & user & "_Temp INNER JOIN tblEmpCourses ON " & _
user & "_Temp.EmpNumber = EmpNo) ON tblCourse.CourseID = tblEmpCourses.CourseID) " & _
"INNER JOIN tblEmp_SuperAdmin ON " & user & "_Temp.EmpNumber = tblEmp_SuperAdmin.EmpNumber" & _
"WHERE (((" & user & "_Temp.EmpNumber) = [Forms]![Reports]![txtEmpID].[Text])) " & _
"ORDER BY CourseName;"
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim rsCmd As ADODB.Command
Set rsCmd = New ADODB.Command
rsCmd.ActiveConnection = CurrentProject.Connection
rsCmd.CommandText = strQuery
rs.Open rsCmd
Me.Recordset = rs
rs.Close
End Sub
This what strQuery contains when I add a breakpoint on rsCmd.CommandText = strQuery:
SELECT myusername_Temp.EmpNumber, [FName]
& ' ' & [LName] AS [Employee Name],
CourseName, DateCompleted,
tblEmp_SuperAdmin.[Cost Centre] FROM
(tblCourse INNER JOIN (myusername_Temp
INNER JOIN tblEmpCourses ON
myusername_Temp.EmpNumber = EmpNo) ON
tblCourse.CourseID=
(It's all one line, but I've written it like this because the underscores italicize the text)
And the error I get says Run Time Error: Join not Supported.
Not quite what I was hoping for, but guessing, for:
strQuery = "long query goes here"
Try:
strQuery = "some long query goes here "
strQuery = strQuery & "more query goes here "
BASED ON NEW INFORMATION:
strQuery = "SELECT " & user & "_Temp.EmpNumber, [FName] & ' ' & [LName] AS [Employee Name], " & _
"CourseName, DateCompleted, tblEmp_SuperAdmin.[Cost Centre] " & _
"FROM (tblCourse " & _
"INNER JOIN tblEmpCourses ON tblCourse.CourseID = tblEmpCourses.CourseID) " & _
"INNER JOIN (Temp INNER JOIN tblEmp_SuperAdmin " & _
"ON Temp.EmpNumber = tblEmp_SuperAdmin.EmpNumber) " & _
"ON Temp.EmpNumber = tblEmpCourses.EmpNo " & _
"WHERE " & user & "_Temp.EmpNumber = " & [Forms]![Reports]![txtEmpID] & _
" ORDER BY CourseName;"
Note that in VBA:
& [Forms]![Reports]![txtEmpID].[Text] &
That is, the reference to the form must go outside the quotes so you get the value.
NEW INFORMATION #2
Your best bet would be to add these tables to the Access query design window and create the joins that you want, then switch to SQL view and use the string generated for you. I do not believe that the string is too long, only that the SQL is incorrect. The SQL I posted above should work, but it may not be what you want.
You can programmatically create a querydef that fits the user. So, when your report is called, you
Delete LoginName_Query_Temp (CurrentDb.QueryDefs.Delete), if it already exists.
Create the querydef (CurrentDB.CreateQueryDef), using LoginName_Temp as the table name.
Set the RecordSource of your Report to LoginName_Query_Temp.
Open the report.
I don't see what purpose the table myusername_Temp serves here. Is that where the name fields are? If so, avoid the join entirely:
Dim lngEmpNumber As Long
Dim strName As String
Dim strSQL As String
lngEmpNumber = Forms!Reports!txtEmpID
strName = DLookup("[FName] & ' ' & [LName]", "myusername_Temp", "EmpNumber=" & lngEmpNumber
strSQL = "SELECT " & Chr(34) & strName & Chr(34) & " AS [Employee Name], " & _
"CourseName, DateCompleted, tblEmp_SuperAdmin.[Cost Centre] " & _
"FROM tblCourse " & _
"INNER JOIN tblEmpCourses " & _
"ON tblCourse.CourseID = tblEmpCourses.CourseID) " & _
"INNER JOIN tblEmp_SuperAdmin " & _
"ON tblEmp_SuperAdmin.EmpNumber = tblEmpCourses.EmpNo " & _
"WHERE tblEmp_SuperAdmin.EmpNumber = " & lngEmpNumber & _
" ORDER BY CourseName;"
Now, the parentheses may need to be changed in the join (I always do my equi-joins in the Access QBE and let it take care of the getting the order and parens correct!), and my assumptions about the purpose of the temp table may be wrong, but I don't see it being used for anything other than as an intermediate link between tables, so I guessed it must be there to provide the name fields.
If that's wrong, then I'm at a loss as to why the temp table needs to be there.
Also, in your second post you referred to the control on the form as:
Forms!Reports!txtEmpID.Text
...the .Text property of Access controls is accessible only when the control has the focus. You could use the .Value property, but since that's the default property of Access controls, you should just stop after the name of the control:
Forms!Reports!txtEmpID
...you'll see this is how I did it in my suggested code.
I find the idea of your name-based temp table to be highly problematic to begin with. Temp tables don't belong in a front end, and it's not clear to me that it is actually a temp table. If it's temp data, put it in a shared table and key the record(s) to the username. Then you don't have to worry about constructing the table name on the fly.