Merge mysql recordset in vba - mysql
I want to use combobox selected value as query to pull another data from mysql database. Let's say Combobox selected value as id. then I will use this id to pull another details related to this id such as supplier, cost etc. to display in label option. I used following code but it doesn't work.
sqlQa = "select Description from matcat_select where BOF like 'MAIN';"
rs.Open sqlQa, oConn, adOpenStatic
With rs
'Set .ActiveConnection = Nothing 'Disconnect the recordset.
k = .Fields.Count
'Populate the array with the whole recordset.
vaData = .GetRows
End With
'Manipulate the Combobox's properties and show the form.
With UserForm1
With .ComboBox1
.Clear
.BoundColumn = k
.List = Application.Transpose(vaData)
.ListIndex = -1
End With
End With
'Manipulate the Combobox's properties and show the form
Set rs1 = CreateObject("ADODB.Recordset")
sqlQb = "Select EOF From matcat_select Where Description = '" & ComboBox1.Value & "';"
rs1.Open sqlQb, oConn
While Not rs1.EOF
Label6.Caption = rs1("EOF")
rs1.MoveNext
Wend
Please check my code and correct me if I am wrong. Also, each time I made query in mysql via vba am I need to establish a connection?
If Not rs1.EOF Then
Label6.Caption = rs1("EOF")
Else
Label6.Caption = ""
End If
Try this
While Not rs1.EOF
Label6.Caption = rs1("EOF")
rs1.MoveNext
Wend
For the select statement
sqlQb = "Select EOF From MyTable Where Description = '" & ComboBox1.Value & "';"
ComboBox is not a text box. To get the selected value, use .Value method.
Yes you need a connection when you want to execute any queries, your VBA is not a database itself, it doesn't knows what you want.
recordset.Open Source, ActiveConnection, CursorType, LockType, Options
http://msdn.microsoft.com/en-us/library/windows/desktop/ms675544(v=vs.85).aspx
Related
VBA SQL Sum Operation in Form
In my form i want to use a sum on row SWS from tblKurse used in the query for the form. On a Button Click event i want to sum the values in that row whenever the criterion in my where is met. Problem is i get an invalid argument error whenever i run it. I just cant find my mistake. Maybe someone can help. Heres the function that i call after the button click event: Public Function chkSWS() As Integer Dim db As Database Dim rst As DAO.Recordset Dim strSQL As String Set db = CurrentDb strSQL = "SELECT SUM(tblKurse.SWS) As Stunden " _ & "FROM tblKurse " _ & "WHERE tblKurse.Dozent_ID = " & Me.cmbKursDOzent Debug.Print strSQL Debug.Print Me.Dozent_ID Set rst = db.OpenRecordset(strSQL, dbForwardOnly) chkSWS = rst![Stunden] Set rst = Nothing Set db = Nothing End Function Error points on the "Set rst" line, so it might been a bad Select statement? printed SQL Statement: SELECT SUM(tblKurse.SWS) As Stunden FROM tblKurse WHERE tblKurse.Dozent_ID = 1
Change this line Set rst = db.OpenRecordset(strSQL, dbForwardOnly) to this Set rst = db.OpenRecordset(strSQL, dbOpenSnapshot)
MS ACCESS how to change a query criteria to look up a record and then create a report
enter image description herei have a program that create field tickets, when the ticket is finished i can see it in a list box name FinishedJobs, when i double click on a ticket inside the listbox it ask me if i want to reopen it or send it to print. The first one (reopen) is done but the second one i can't get it to work. The problem is i have the ticket number in a variable named strCriteria and i want to use that value and put it in the criteria inside the query name JobsTicketGeneralReport, so i can open a report using that query. PLEASE HELP ME TO CHANGE THE CRITERIA IN THE QUERY TO SEARCH MY TICKET NUMBER. I'M WILLING TO CHANGE THE CODES IF YOU SUGGEST THAT. NOTE: My query is a combine query it has 6 tables and has the ticket number in common, when i call the ticket number it bring the information of all tables. This what i am doing: Dim db As DAO.Database Dim qdf As DAO.QueryDef Dim rst As Recordset Dim varItem As Variant Dim strCriteria As String Dim qdfOld As String Dim strSQL As String ' Get the database and stored query Set db = CurrentDb() Set qdf = db.QueryDefs("JobsticketGeneralReport") ' Loop through the selected items in the list box and build a text string For Each varItem In Me!List0.ItemsSelected strCriteria = strCriteria & ",'" & Me.List0.Column(0) & "'" Next varItem ' Check that user selected something If Len(strCriteria) = 0 Then MsgBox "You did not select anything from the list" _ , vbExclamation, "Nothing to find!" Exit Sub End If 'Debug.Print strCriteria ' Remove the leading comma from the string strCriteria = Right(strCriteria, Len(strCriteria) - 1) Debug.Print strCriteria ' change criteria in query qdf.Parameters(0).Value = Trim(strCriteria) Set rst = qdf.OpenRecordset DoCmd.OpenQuery "JobsticketgeneralReport" DoCmd.OpenReport "JobsticketgeneralReport", acpreview rst.Close qdf.Close Set rst = Nothing Set qdf = Nothing HERE IS MY SQL: SELECT JobsOrder.StartDigDate, JobsOrder.Ticket, JobsOrder.DigNumber, JobsOrder.JobType, JobsOrder.JobAddressNumber, JobsOrder.JobAddressName, JobsOrder.JobAddressTown, JobsOrder.JobDescription, JobsOrder.AssetID, JobsOrder.Notes, JobsOrder.FINISH, JobsOrder.updateGIS, JobsOrder.Priority, GENERAL.STARTJOBDATE, GENERAL.ENDJOBDATE, GENERAL.DAY1, GENERAL.DAY2, GENERAL.EMPLOYEE0, GENERAL.EMPLOYEE1, GENERAL.EMPLOYEE2, GENERAL.EMPLOYEE3, GENERAL.EMPLOYEE4, GENERAL.EMPLOYEE5, GENERAL.EMPLOYEE6, GENERAL.EMPLOYEE7, GENERAL.VEHICLE0, GENERAL.VEHICLE1, GENERAL.VEHICLE2, GENERAL.VEHICLE3, GENERAL.VEHICLE4, GENERAL.VEHICLE5, GENERAL.EMPLOYEE0TIME, GENERAL.EMPLOYEE1TIME, GENERAL.EMPLOYEE2TIME, GENERAL.EMPLOYEE3TIME, GENERAL.EMPLOYEE4TIME, GENERAL.EMPLOYEE5TIME, GENERAL.EMPLOYEE6TIME, GENERAL.EMPLOYEE7TIME, GENERAL.DRAWINGATT, GENERAL.FINISH, GENERAL.ASPHALT, GENERAL.ROW, GENERAL.CONCRETE, GENERAL.DIRT, GENERAL.TRENCH, MAINS.[JOBTYPE-MAIN], MAINS.MATERIAL, MAINS.SIZE, MAINS.DEPTH, MAINS.INTERNALCONDITION, MAINS.COMMENTS, MAINS.REPAIRLOCATION, MAINS.LOCATION1, MAINS.LOCATION2, MAINS.MATERIAL1, MAINS.MATERIAL2, MAINS.MATERIAL3, MAINS.MATERIAL4, MAINS.MATERIAL5, MAINS.MATERIAL6, MAINS.MATERIAL7, MAINS.MATERIAL8, MAINS.MATERIAL9, MAINS.MATERIAL10, MAINS.MATERIAL11, MAINS.MATERIAL12, MAINS.QTY1, MAINS.QTY2, MAINS.QTY3, MAINS.QTY4, MAINS.QTY5, MAINS.QTY6, MAINS.QTY7, MAINS.QTY8, MAINS.QTY9, MAINS.QTY10, MAINS.QTY11, MAINS.QTY12, MAINS.ENABLE, SERVICES.JOBPERFORMBY, SERVICES.SERVICEASSET, SERVICES.OFFON, SERVICES.[MATERIAL-MC], SERVICES.[SIZE-MC], SERVICES.[DEPTH-MC], SERVICES.[MATERIAL-CB], SERVICES.[SIZE-CB], SERVICES.[DEPTH-CB], SERVICES.CURBBOXLOCATION, SERVICES.LOCATION1, SERVICES.LOCATION2, SERVICES.LOCATION3, SERVICES.[SERVICE-COMMENT], SERVICES.[MATERIAL1-MC], SERVICES.[MATERIAL2-MC], SERVICES.[MATERIAL3-MC], SERVICES.[MATERIAL4-MC], SERVICES.[MATERIAL5-MC], SERVICES.[MATERIAL6-MC], SERVICES.[MATERIAL7-MC], SERVICES.[MATERIAL8-MC], SERVICES.[QTY1-MC], SERVICES.[QTY2-MC], SERVICES.[QTY3-MC], SERVICES.[QTY4-MC], SERVICES.[QTY5-MC], SERVICES.[QTY6-MC], SERVICES.[QTY7-MC], SERVICES.[QTY8-MC], SERVICES.[MATERIAL1-CB], SERVICES.[MATERIAL2-CB], SERVICES.[MATERIAL3-CB], SERVICES.[MATERIAL4-CB], SERVICES.[MATERIAL5-CB], SERVICES.[MATERIAL6-CB], SERVICES.[MATERIAL7-CB], SERVICES.[MATERIAL8-CB], SERVICES.[QTY1-CB], SERVICES.[QTY2-CB], SERVICES.[QTY3-CB], SERVICES.[QTY4-CB], SERVICES.[QTY5-CB], SERVICES.[QTY6-CB], SERVICES.[QTY7-CB], SERVICES.[QTY8-CB], SERVICES.REPAIR, SERVICES.Replace, SERVICES.INSTALL, SERVICES.REMOVE, SERVICES.TEMPDISCONNECT, SERVICES.ENABLE, HYDRANT.[ENABLE-H], HYDRANT.[HYDRANT-ASSET], HYDRANT.[REPAIR-H], HYDRANT.[REPLACE-H], HYDRANT.[INSTALL-H], HYDRANT.FLUSH, HYDRANT.FLOWTEST, HYDRANT.PARTS1, HYDRANT.PARTS2, HYDRANT.PARTS3, HYDRANT.PARTS4, HYDRANT.PARTS5, HYDRANT.PARTS6, HYDRANT.PARTS7, HYDRANT.PARTS8, HYDRANT.[QTY1-H], HYDRANT.[QTY2-H], HYDRANT.[QTY3-H], HYDRANT.[QTY4-H], HYDRANT.[QTY5-H], HYDRANT.[QTY6-H], HYDRANT.[QTY7-H], HYDRANT.[QTY8-H], HYDRANT.JOBPERFORM, HYDRANT.[MANUFACTORY OLD], HYDRANT.MANUFACTORY, HYDRANT.SIZENEW, HYDRANT.SIZEOLD, HYDRANT.JOBNOTES, HYDRANT.TIMEOPEND, HYDRANT.TIMECLOSED, HYDRANT.TIMETOCLEAR, HYDRANT.COLOROPEN, HYDRANT.COLORCLOSE, HYDRANT.REMARKS, HYDRANT.[STATIC-PRESSURE], HYDRANT.[RESIDUAL-PRESSURE], HYDRANT.[PITOT-TESTFLOWRATE], HYDRANT.CAPACITY, HYDRANT.[ASSET-ID1], HYDRANT.[ASSET-ID2], VALVES.ENABLE, VALVES.[REPAIR-V], VALVES.[REPLACE-V], VALVES.[INSTALL-V], VALVES.[REMOVE-V], VALVES.[MAINTENANCE-V], VALVES.VALVECOMMENT, VALVES.[MATERIAL1-V], VALVES.[MATERIAL2-V], VALVES.[MATERIAL3-V], VALVES.[MATERIAL4-V], VALVES.[MATERIAL5-V], VALVES.[MATERIAL6-V], VALVES.[QTY1-V], VALVES.[QTY2-V], VALVES.[QTY3-V], VALVES.[QTY4-V], VALVES.[QTY5-V], VALVES.[QTY6-V], VALVES.[LOCATION1-V], VALVES.[LOCATION2-V], VALVES.[LOCATION3-V], VALVES.[LOCATION4-V], VALVES.VALVE1, VALVES.VALVE2, VALVES.VALVE3, VALVES.VALVE4, VALVES.VALVE5, VALVES.VALVE6, VALVES.VALVE7, VALVES.VALVE8, VALVES.VALVEPOSITION1, VALVES.VALVEPOSITION2, VALVES.VALVEPOSITION3, VALVES.VALVEPOSITION4, VALVES.VALVEPOSITION5, VALVES.VALVEPOSITION6, VALVES.VALVEPOSITION7, VALVES.VALVEPOSITION8, VALVES.[VALVE-TURNS1], VALVES.[VALVE-TURNS2], VALVES.[VALVE-TURNS3], VALVES.[VALVE-TURNS4], VALVES.[VALVE-TURNS5], VALVES.[VALVE- TURNS6], VALVES.[VALVE-TURNS7], VALVES.[VALVE-TURNS8], VALVES.[VALVE-DEPTH1], VALVES.[VALVE-DEPTH2], VALVES.[VALVE-DEPTH3], VALVES.[VALVE-DEPTH4], VALVES.[VALVE-DEPTH5], VALVES.[VALVE-DEPTH6], VALVES.[VALVE-DEPTH7], VALVES.[VALVE-DEPTH8], VALVES.REASON1, VALVES.REASON2, VALVES.REASON3, VALVES.REASON4, VALVES.REASON5, VALVES.REASON6, VALVES.REASON7, VALVES.REASON8, INSPECT.ENABLE, INSPECT.[CURBBOX-I], INSPECT.[VALVEBOX-I], INSPECT.[SERVICE-I], INSPECT.CURBBOXREMARKS, INSPECT.VALVEBOXREMARKS, INSPECT.SERVICEREMARKS FROM (((((JobsOrder INNER JOIN [GENERAL] ON JobsOrder.Ticket = GENERAL.TICKET) INNER JOIN MAINS ON GENERAL.TICKET = MAINS.TICKET) INNER JOIN SERVICES ON MAINS.TICKET = SERVICES.TICKET) INNER JOIN HYDRANT ON SERVICES.TICKET = HYDRANT.TICKET) INNER JOIN VALVES ON HYDRANT.TICKET = VALVES.TICKET) INNER JOIN INSPECT ON VALVES.TICKET = INSPECT.TICKET WHERE (((JobsOrder.Ticket)=[ticket]) AND ((JobsOrder.FINISH)=True)) ORDER BY JobsOrder.StartDigDate, JobsOrder.Ticket;
If you want to use a parameter in the query, you should explicitly define it. Also, it is a good idea to give the parameter a different name than the involved tables and fields. To do this, use the "Parameters" window in query design, or add a PARAMETERS clause to the beginning of the SQL: PARAMETERS parTicket Text ( 255 ); SELECT ..... and in the WHERE clause WHERE (((JobsOrder.Ticket)=[parTicket]) This is mainly useful if you want to read data from the query in VBA, i.e. you need this for Set rst = qdf.OpenRecordset But if the query is RecordSource for a report, this won't work, because the report opens its own instance of the query. In this case, you need Parfait's solution: directly use the listbox in the query. WHERE ((JobsOrder.Ticket) = Forms!yourForm!List0)
For Each varItem In Me!List0.ItemsSelected strCriteria = strCriteria & ",'" & Me.List0.Column(0) & "'" Next varItem This cannot work - you must use varItem in the loop. Me.List0.Column(0) will always pick the same element. Debug.Print strCriteria This should have told you what went wrong.
display a table field in a list using condition
I have table x that contains id and name fields. I want to display the id when I select the name in the list. I wrote this but it doesn't work. The error msg is: either BOF or EOF or current record has been deleted. Requested operation requires a current record. I think simply the default record is record 1, so what's wrong ?! Dim con As Connection Dim rs As New Recordset Set con = CurrentProject.Connection rs.Open "select id from tbl where namen = '" & list1.ListIndex & "'", con, adOpenDynamic, adLockOptimistic ttt.SetFocus ttt.Text = rs!id thank you so much pteranodon for your help I changed the code to be like this Private Sub list1_Click() Dim strSQL As String strSQL = "select id from tbl where namen = '" & list1.Value & "'" ttt.SetFocus ttt = DLookup("id", "tbl", "namen='" & list1.Value & "'") rs.Open strSQL, con, adOpenDynamic, adLockOptimistic End Sub but I got this msg operation is not allowed when the object is open ? I didn't add the items to the list1 by using vba code I just followed the window that show up after adding the list1 to the form cus I also have problem with code if you please can you add the complete code 1 and 2
You want the value of the listbox, not ListIndex. ListIndex contains a number, the zero-based index of the current selection in the listbox. You are passing in something like select id from tbl where namen = '13' Since no records match, you get that error message. I really reccommend using a string to hold any constructed SQL so that you can debug it easily. If you had Dim strSQL As String strSQL = "select id from tbl where namen = '" & list1.Value & "'" Debug.Print strSQL rs.Open strSQL, con, adOpenDynamic, adLockOptimistic it would be easier to read and much easier to debug. Also, you'll want to check for rs.BOF and rs.EOF right after opening a recordset: If Not (rs.BOF Or rs.EOF) Then 'Do stuff Else 'No records in recordset End Unlike VB textboxes, you can't use .Text in VBA textboxes unless the textbox has the focus. Use ttt.Value (or just ttt instead). And if you are only looking up a single value like this you can replace all of your code like this: Private Sub list1_Click() ttt = DLookup("id", "tbl", "namen='" & list1.Value & "'") End Sub Using DLookup instead of manually opening a recordsest yourself. I would also go back through the listbox wizard. If you put the id in the first column and the name in the second, then hide the first column (the wizard will help you do this), the list will show the name but store the id. Then you don't even need the extra textbox. The id is stored in List1.Value and the name is available as List1.Column(1).
Setting focus in MS Access
I am creating a recordset from a Qdefs and then displaying the records in a form. When I filter the values, focus is going to the first record. But, I want the focus to point to the same record that was in focus before filtering. This is how am creating a recordset from an existing querydefs before and after filtering db.QueryDefs("Query_vinod").Sql = filter Set rs_Filter_Rowsource = db.OpenRecordset("Abfr_SSCI_Check_Findings_List")
I think you can do this by using a bookmark. Set up a RecordsetClone and then find your active record by using the FindFirst method. I have some sample code that will need to be modified a little to fit your exact variables: Dim Rs As Recordset Dim Test As Integer Dim varBookmark As Variant DoCmd.OpenForm "Contracts" Set Rs = Forms!Contracts.RecordsetClone Rs.FindFirst ("[ID] = '" & Me![ID] & "'") varBookmark = Rs.Bookmark Forms!Contracts.Form.Bookmark = varBookmark If Rs.NoMatch Then MsgBox "That does not exist in this database." Else End If
Access pass-through query giving error
I'm using the following code to run a passthrough query. I'm trying to run the passthrough query, then check how many records are returned to figure out if it worked. But I get an error saying "Invalid Operation" Why is it doing this and how can I correct? Dim Item As String Item = InputBox("Enter Item needing a surrogate UPC.", "Enter Item") Set db = CurrentDb Set qdf = db.QueryDefs("spAL_AssignSurrogateUPC") qdf.ReturnsRecords = True qdf.SQL = "spAL_AssignSurrogateUPC '" & Item & "'" With qdf.OpenRecordset(dbOpenSnapshot) '<--- Error triggered here. If qdf.RecordCount = 1 Then MsgBox "Surrogate UPC assigned." Else MsgBox "ERROR. Could not assign surrogate UPC." End If End With
Use the EXEC syntax qdf.SQL = "EXEC spAL_AssignSurrogateUPC '" & Replace(Item, "'", "''") & "';" (I assume it's SQL Server.) I also added a replace function in order to cope with single quotes in the Item string. This also helps prevent SQL injections. EDIT: Try to do this, instead of using the With-statement Dim rs as DAO.Recordset ... Set rs = qdf.OpenRecordset(dbOpenSnapshot) If rs.EOF Then MsgBox "ERROR. Could not assign surrogate UPC." Else MsgBox "Surrogate UPC assigned." End If