473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Create sql query using listbox items

I am new in vba and specially vba query. My question is following:-

I have a form named "frmCreateQry" which have following
FieldType FieldName
1. ComboBox cboSelectTblQry
2. ListBox lstSelectFrom
3. ListBox lstSelectTo
4. Button cmdMove
5. Button cmdExport

cboSelectTblQry rowsource have form Onload value. It contains all table names of my database. When I select any table name from this combobox then all related fields name display in lstSelectFrom (ListBox). If I select any fields from lstSelectFrom and click on cmdMove (Button) then selected fields moves to lstSelectTo.

My question is that I want to create query using lstSelectTo items/fields by clicking cmdExport (Button) and export this query to excel format.

Kindly tell me the process.

Thanks
Attached Images
File Type: jpg frmCreateQry.jpg (38.1 KB, 735 views)
Feb 8 '16 #1
9 3936
Seth Schrock
2,965 Expert 2GB
You will need to loop through your list box's selected items and concatenate them into a string separated by commas. Then just get the name of the table from your combo box and you can build the query SQL string. To export it, I'm pretty sure that you have to then pass the SQL string to a querydef, save it and then export the query.
Feb 8 '16 #2
I prepared this... but not working


Expand|Select|Wrap|Line Numbers
  1. Dim dbs As Database
  2. Dim qdf As DAO.QueryDef
  3. Dim Tbl As DAO.TableDef
  4. Dim varItem As Variant
  5. Dim strCriteria As String
  6. Dim strSQL As String
  7.  
  8. Set dbs = CurrentDb()
  9. Set qdf = dbs.CreateQueryDef("MyQry")
  10. Set Tbl = dbs.CreateTableDef(Me.cboSelectTblQry.Value)
  11. For Each varItem In Me!lstSelectTo.ItemsSelected
  12. strCriteria = strCriteria & "," & Me!lstSelectTo.ItemData(varItem) & ""
  13. Next varItem
  14. If Len(strCriteria) = 0 Then
  15. MsgBox "You did not select anything." _
  16. , vbExclamation, "Nothing to find!"
  17.  
  18. Exit Sub
  19. End If
  20.  
  21. strCriteria = Right(strCriteria, Len(strCriteria) - 1)
  22. strSQL = "SELECT* FROM Tbl "
  23.  
  24. DoCmd.OpenQuery "MyQry"
  25. Set db = Nothing
  26. Set qdf = Nothing
Feb 8 '16 #3
can you tell me the code as you described....
Feb 8 '16 #4
Seth Schrock
2,965 Expert 2GB
After line 21, put Debug.Print strCriteria and post the result (it will display in the immediate window. Press Ctrl + G if it isn't visible.). Then, your strSQL isn't accurate. Assuming that strCriteria is the WHERE clause, it would need to be
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT * FROM " & Me.cboSelectTblQry & " WHERE " & strCriteria
Then you need to edit the SQL of a query.
Expand|Select|Wrap|Line Numbers
  1. Set qdf = db.QueryDef("MyQry")
  2. qdf.SQL = strSQL
  3. qdf.Close
Now you can export the query. I use the DoCmd.TransferSpreadsheet method for which you can get the instructions from the MSDN website.
Feb 8 '16 #5
debug.print strCriteria

result

Emp_Design,Emp_Gender,Emp_Lname,Emp_FName,ID,Offic e,UserDA



but error " Method or Data member not found" with following line
Expand|Select|Wrap|Line Numbers
  1. Set qdf = dbs.QueryDef("MyQry")
My Full code is:-

Expand|Select|Wrap|Line Numbers
  1. Dim dbs As Database
  2. Dim qdf As DAO.QueryDef
  3. Dim varItem As Variant
  4. Dim strCriteria As String
  5. Dim strSQL As String
  6.  
  7. Set dbs = CurrentDb()
  8.  
  9. For Each varItem In Me!lstSelectTo.ItemsSelected
  10. strCriteria = strCriteria & "," & Me!lstSelectTo.ItemData(varItem) & ""
  11. Next varItem
  12.  
  13. If Len(strCriteria) = 0 Then
  14. MsgBox "You did not select anything." _
  15. , vbExclamation, "Nothing to find!"
  16. Exit Sub
  17. End If
  18.  
  19. strCriteria = Right(strCriteria, Len(strCriteria) - 1)
  20. Debug.Print strCriteria
  21. strSQL = "SELECT * FROM " & Me.cboSelectTblQry & " WHERE " & strCriteria
  22.  
  23. Set qdf = dbs.QueryDef("MyQry")
  24. qdf.sql = strSQL
  25. qdf.Close
  26.  
  27. Set dbs = Nothing
Feb 9 '16 #6
Hello seth,

I resolve previous error.. Now syntex error "3075"

i attached image for same here.
Attached Images
File Type: jpg Error Msg.jpg (48.6 KB, 354 views)
File Type: jpg Error.jpg (48.9 KB, 352 views)
Feb 9 '16 #7
Seth Schrock
2,965 Expert 2GB
Sorry it has taken me so long to get back with you.

Now that you have resolved the previous error, please put a Debug.Print strSQL on line 22 and post back what it puts in the immediate window. Based on the error that you are receiving, there is a problem with your SQL string.
Feb 12 '16 #8
ADezii
8,834 Expert 8TB
I create the following simple Code Segment that will do exactly as you requested and will also allow for Spaces in Table and Field Names. For the sake of brevity and conciseness, I included neither Validation or Error Checking:
Expand|Select|Wrap|Line Numbers
  1. Dim strTableName As String
  2. Dim strBaseSQL As String
  3. Dim strCriteria As String
  4. Dim varItem As Variant
  5. Dim strSQL As String
  6. Dim qdf As DAO.QueryDef
  7.  
  8. strTableName = Me![cboSelectTblQry]
  9. strBaseSQL = "SELECT "
  10.  
  11. For Each varItem In Me![lstSelectTo].ItemsSelected
  12.   strCriteria = strCriteria & "[" & Me![lstSelectTo].ItemData(varItem) & "],"
  13. Next
  14.  
  15. strSQL = strBaseSQL & Left$(strCriteria, Len(strCriteria) - 1) & " FROM [" & strTableName & "]"
  16.  
  17. Set qdf = CurrentDb.CreateQueryDef("MyQry", strSQL)
  18.  
  19. RefreshDatabaseWindow
  20.  
  21. 'All you have to do is Export MyQry here
Feb 14 '16 #9
Thanks to Seth and ADezii...

ADezii code work fine after some editing.

But now one more problem is that...
After export in excel to MyQry, Date fields shows numerical format instead of Date Format in excel but date field shows correct date format in MyQry in query section.


Any Suggestion..

My Code is following:-

Expand|Select|Wrap|Line Numbers
  1. If cboFormat = "Excel" Then
  2. Dim strTableName As String
  3. Dim strBaseSQL As String
  4. Dim strCriteria As String
  5. Dim varItem As Variant
  6. Dim strSQL As String
  7. Dim qdf As DAO.QueryDef
  8. Dim OutPut As String
  9.  
  10.  
  11. For Each qdf In CurrentDb.QueryDefs
  12.   If qdf.Name = "MyQry" Then
  13.     DoCmd.DeleteObject acQuery, "MyQry"
  14.      Exit For
  15.   End If
  16. Next
  17.  
  18. strTableName = Me![cboSelectTblQry]
  19. strBaseSQL = "SELECT "
  20.  
  21. For Each varItem In Me![lstSelectTo].ItemsSelected
  22.   strCriteria = strCriteria & "[" & Me![lstSelectTo].ItemData(varItem) & "],"
  23. Next
  24.  
  25. If Len(strCriteria) = 0 Then
  26. MsgBox "You did not select anything." _
  27. , vbExclamation, "Nothing to find!"
  28. Exit Sub
  29. End If
  30.  
  31. strSQL = strBaseSQL & Left$(strCriteria, Len(strCriteria) - 1) & " FROM [" & strTableName & "]"
  32.  
  33. Set qdf = CurrentDb.CreateQueryDef("MyQry", strSQL)
  34.  
  35. OutPut = "D:/Export.xlsx"
  36. DoCmd.TransferSpreadsheet acExport, , "MyQry", OutPut
  37. MsgBox " File has been exported to " & OutPut
  38.  
  39. RefreshDatabaseWindow
  40. End If
  41.  
  42. ExitSub:
  43.     Exit Sub
  44. ErrorHandler:
  45.     Resume ExitSub
Feb 20 '16 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Paxton | last post by:
In the same way that you can create and alter permanent tables in Access 2000 using SQL in ASP ( CREATE TABLE <tablename> col_1(col_1 datatype), col_2(col_2 datatype....Col_n(col_n datatype)) is...
6
by: Valerian John | last post by:
I have a ListBox webcontrol on an aspx page. Items are added to the ListBox using client-side code. However, when the page is posted back the items are missing/not available. (It is like the...
3
by: Stimp | last post by:
I have a listbox of values that I populate from a database. I want the user to be able to re-order the list (by first selecting an item and then clicking 'up' or 'down' buttons) and then save...
3
by: Baren | last post by:
Hi! I have a generalized Stored Procedure to get the listbox items in a datareader.Then i am binding the datareader to the listbox. For different pages and different conditions i need to hide...
13
by: forbes | last post by:
Hi, I have a user that used the Query Wizard to create a query in Access. Now she claims that her master table is missing all the data that was excluded from the query. Can you create anything...
3
by: Brian Simmons | last post by:
Hi All, I search on codeproject and google but didn't find what I was looking for. Anybody know of a good implementation where you have 2 listboxes and you want to move items between the 2. ...
1
by: Refugnic | last post by:
I tried to fill a ListBox with a DataSource pointing to an ArrayList. It all works fine...up to one point. The ArrayList is dynamic, which means the contents of it change, during the course of...
6
by: kimiraikkonen | last post by:
Hi, I can read from a text file and insert text file's contents into a listbox whose format is line by line using this code: Dim reader As String reader = My.Computer.FileSystem.ReadAllText("c:...
1
by: divya | last post by:
Hello, I used the below code to move selected items between two listboxes Left and Right:- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPopup.aspx.cs" Inherits="TestPopup" %> ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.