473,465 Members | 1,444 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Multi-select list box query

7 New Member
I have read the other posts on this subject, but I am still having issues.

I have a few combo, and one multiselect list box for States delimiting. I am trying to feed a query based on this info. I have it working as a combo box, but now I am getting "type mismatch" and "object variable or with block variable not set" errors.

Help would be much appreciated in the "listbox" commented section and the stSQL definition.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Dim db As DAO.Database
  3. Dim qdf As DAO.QueryDef
  4. Dim rs As DAO.Recordset
  5. Dim prm As DAO.Parameter
  6. Private Sub Run_Click()
  7. On Error GoTo Err_Run_Click
  8.     Dim SpreadSheetSource As String
  9.     Dim SpreadSheetOutput As String
  10.     Dim stTerritory As String
  11.     Dim stState As String
  12.     Dim stStatePrint As String
  13.     Dim stBrand As String
  14.     Dim dFirstMonth As Date
  15.     Dim stDocName As String
  16.     Dim Suc As Boolean
  17.     Dim varItem As Variant
  18.     Dim listItems As Control
  19.     Dim stSQL As String
  20.  
  21.     'Data definitions
  22.     'SpreadSheetSource = "S:\__2 2009 Budgets\Sales and Marketing expenses\Reports\Profit loss Flow.xls"
  23.     'SpreadSheetOutput = "S:\__2 2009 Budgets\Sales and Marketing expenses\Reports\Profit loss Flow_new.xls"
  24.     SpreadSheetSource = "Y:\Documents\Freelance\Cape Classics\V6\Profit loss Flow.xls"
  25.     SpreadSheetOutput = "Y:\Documents\Freelance\Cape Classics\V6\Profit loss Flow_new.xls"
  26.  
  27.  
  28.     stDocName = "output"
  29.         dFirstMonth = Me.firstmonth
  30.  
  31.     'list box
  32.     Set listItems = Me.State
  33.     For Each varItem In listItems.ItemsSelected
  34.         If stState > "" Then
  35.             stState = stState & " And (([zz Profit and Loss Flow Final].[State]) Like """ * """ &" & listItems.ItemData(varItem) & "& """ * """)"
  36.             stStatePrint = stStatePrint & ", " & listItems.ItemData(varItem)
  37.         Else
  38.             stState = " And (([zz Profit and Loss Flow Final].[State]) Like """ * """ &" & listItems.ItemData(varItem) & "& """ * """)"
  39.             stStatePrint = listItems.ItemData(varItem)
  40.         End If
  41.         MsgBox (stSate)
  42.     Next varItem
  43.  
  44.     stSQL = "SELECT [zz Profit and Loss Flow Final].[Year-Month], Sum([zz Profit and Loss Flow Final].[Cases Shipped]) AS [Cases Shipped], Sum([zz Profit and Loss Flow Final].[Cases Depl]) AS [Cases Depl], Sum([zz Profit and Loss Flow Final].[Cases Depl Budget]) AS [Cases Depl Budget], Sum([zz Profit and Loss Flow Final].[Gross Profit]) AS [Gross Profit], Sum([zz Profit and Loss Flow Final].[SPAs]) AS SPAs, Sum([zz Profit and Loss Flow Final].[Samples]) AS Samples, Sum([zz Profit and Loss Flow Final].[Other Selling Exp]) AS OtherSellingExp, Sum([zz Profit and Loss Flow Final].[Salaries]) AS Salaries, Sum([zz Profit and Loss Flow Final].[Travel & Enter]) AS [Travel & Enter], Sum([zz Profit and Loss Flow Final].[Primary Budget]) AS [Primary Budget], Sum([zz Profit and Loss Flow Final].[T&E Bud]) AS [T&E Bud], Sum([zz Profit and Loss Flow Final].[Add'l Budget]) AS [Add'l Budget], Sum([zz Profit and Loss Flow Final].[Sal Budget]) AS [Sal Budget] INTO storeOutput" & vbCrLf & _
  45.             "FROM [zz Profit and Loss Flow Final]" & vbCrLf & _
  46.             "WHERE ((([zz Profit and Loss Flow Final].[Region]) Like " & Chr(34) & " * " & Chr(34) & " & [Forms]![Sort].[Territory] & " & Chr(34) & " * " & Chr(34) & ") And (([zz Profit and Loss Flow Final].[State]) Like " & Chr(34) & "*" & Chr(34) & " & [Forms]![Sort].[State] & " & Chr(34) & " * " & Chr(34) & ") And (([zz Profit and Loss Flow Final].[Brand]) Like " & Chr(34) & " * " & Chr(34) & " & [Forms]![Sort].[Brand] & " & Chr(34) & " * " & Chr(34) & "))" & vbCrLf & _
  47.             "GROUP BY [zz Profit and Loss Flow Final].[Year-Month];"
  48.  
  49.  
  50.     If Me.Territory <> "" Then
  51.         stTerritory = Me.Territory
  52.     Else
  53.         stTerritory = "All"
  54.     End If
  55.     If stStatePrint = "" Then
  56.         stState = "All"
  57.     End If
  58.     If Me.Brand <> "" Then
  59.         stBrand = Me.Brand
  60.     Else
  61.         stBrand = "All"
  62.     End If
  63.  
  64.     DoCmd.SetWarnings False
  65.     'DoCmd.OpenQuery "makeOutput"
  66.     DoCmd.SetWarnings True
  67.  
  68.     'create the recordset
  69.     Set db = CurrentDb()
  70.     db.Execute stSQL, dbFailOnError
  71.     'Set qdf = db.CreateQueryDef("MakeOutput2", stSQL)
  72.  
  73.  
  74.     'Query Parameters
  75.     'qdf.Parameters("Region") = Me.Territory
  76.     'qdf.Parameters("State") = Me.State
  77.     'qdf.Parameters("Brand") = Me.Brand
  78.  
  79.     'Resolve the Parameters
  80.     'For Each prm In qdf.Parameters
  81.     '   prm.Value = Eval(prm.Name)
  82.     'Next
  83.     Set rs = db.OpenRecordset("storeOutput")
  84.  
  85.     'Set rs = qdf.OpenRecordset()
  86.     'rs.MoveLast: rs.MoveFirst
  87.  
  88.     If rs.RecordCount = 0 Then Exit Sub
May 12 '09 #1
3 2619
CyberSoftHari
487 Recognized Expert Contributor
You have to user ‘New’ Instances to create objects
Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. 'To
  3. Dim db As New DAO.Database
  4.  
May 13 '09 #2
kirschey
7 New Member
I now get, "Invalid use of New keyword"
May 13 '09 #3
QVeen72
1,445 Recognized Expert Top Contributor
Hi,

For DAO, not Necessary to Instantiate with "New" keyword..

Just try this, Wrap the Sates with Single Quotes, Instead of Double..:
And Use an "OR"

stState = stState & " ([zz Profit and Loss Flow Final].[State]) Like '*" & listItems.ItemData(varItem) & "*) ' OR "

finally strip off the last OR
If Trim(stState) <> "" Then
stState = Left(stState, Len(stState)-3)
End If

Also, In Your SQL Statement,

Remove this Part :
And (([zz Profit and Loss Flow Final].[State]) Like " & Chr(34) & "*" & Chr(34) & " & [Forms]![Sort].[State]

' No Need To Mention Again Field Name, It is already included in stState
Replace It With : And " & stState

Change the same for all the Fields


Regards
Veena
May 14 '09 #4

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

Similar topics

37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
4
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
17
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, Wide character and multi-byte character are two popular encoding schemes on Windows. And wide character is using unicode encoding scheme. But each time I feel confused when...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
2
by: Aussie Rules | last post by:
Hi, I have a site that Iwant to either display my text in english or french, based on the users prefernces ? I am new to webforms, but I know in winforms, this is pretty easy with a resource...
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
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...
1
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.