Solved

Need assistance with modifying SQL statement (in form)

Posted on 2014-11-20
6
138 Views
Last Modified: 2014-11-20
Experts:

I need some assistance with tweaking a query (generated which is generated via a form).

Please see attached database (zipped).  In contains the following objects:

1. Table1
2. Query1
3. frmLogin

Process upon opening:
1. Open form "frmLogin"
2. Select any of the three values from the listbox
3. This will open Query1 and display the associated responses.

Here's what I need some help with:
- Tweak the SQL (in frmLogin) so that the selected value from the listbox (e.g., "Display 2-digit numbers)
is added as records (in new column [Selection]).
- Attached JPG "Query -- Current and New" illustrates the current and new query views.

Any idea as to how the SQL can be modified to achieve the new query display?

Thanks,
EEH
Query.zip
Query----Current-and-New.JPG
0
Comment
Question by:ExpExchHelp
  • 3
  • 2
6 Comments
 
LVL 12

Accepted Solution

by:
James Elliott earned 500 total points
ID: 40455233
Maybe this:

sql = "SELECT [" & strGenericField & "] as X_Digit, '" & Me.ListBoxTest.Value & "' as Selection FROM Table1"

Open in new window

0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40455239
Public Sub ListBoxTest_AfterUpdate()

On Error Resume Next
       Dim qd As DAO.QueryDef, db As DAO.Database, sql As String
       Dim strGenericField As String
       Set db = CurrentDb
       Set qd = db.QueryDefs("Query1")
       
       Select Case Me.ListBoxTest
            Case "Display 2-digit numbers": strGenericField = "2-DigitNumber"
            Case "Display 3-digit numbers": strGenericField = "3-DigitNumber"
            Case "Display 4-digit numbers": strGenericField = "4-DigitNumber"
       End Select
       
'       TempVars.Add "GenericField", Me.ListBoxTest.Value  <<< REVISED see next line
       TempVars.Add "GenericField", strGenericField
       sql = "Select ([" & strGenericField & "]) AS X_Digit FROM Table1"
       qd.sql = sql
       
       DoCmd.OpenQuery "Query1"

End Sub
0
 

Author Closing Comment

by:ExpExchHelp
ID: 40455240
James -- thank you... your solution is perfect!
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:ExpExchHelp
ID: 40455245
Rey -- I just noticed your response.   I already had tested James' solution before I saw your feedback.

Thank you for chiming in... points already had been awarded though.  

Again, I thank you both for your assistance.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40455249
correction, copied the wrong codes

Public Sub ListBoxTest_AfterUpdate()

On Error Resume Next
       Dim qd As DAO.QueryDef, db As DAO.Database, sql As String
       Dim strGenericField As String
       Set db = CurrentDb
       Set qd = db.QueryDefs("Query1")
       
       Select Case Me.ListBoxTest
            Case "Display 2-digit numbers": strGenericField = "2-DigitNumber"
            Case "Display 3-digit numbers": strGenericField = "3-DigitNumber"
            Case "Display 4-digit numbers": strGenericField = "4-DigitNumber"
       End Select
       
'       TempVars.Add "GenericField", Me.ListBoxTest.Value
       TempVars.Add "GenericField", strGenericField
'       sql = "Select ([" & strGenericField & "]) AS X_Digit FROM Table1"   <<< REVISED see next line
        sql = "SELECT [" & strGenericField & "] as X_Digit, '" & Me.ListBoxTest.Value & "' as Selection FROM Table1"
       qd.sql = sql
       
       DoCmd.OpenQuery "Query1"

End Sub
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40455251
np...
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now