473,406 Members | 2,633 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,406 software developers and data experts.

If- then statement to check if input is from a lookup column

Hi, all

I'm having trouble figuring out how to use an if-then statement to check whether or not a variable is pointing to a look-up column.

What I have is a form that contains a combobox and a text box. The combobox contains the list of fields available in my table. When a field is selected, the user then enters text in to the text box. These two inputs create a query.

The problem is that some of my fields are look-up columns, and some are not. If I insert ".value" into the SQL, it works great IF the field is a look-up column, but not if it's just a regular field. If I remove the ".value", it works great IF the field is a regular field, but not if it is a look-up column.

So is there a way to say:
If a = look-up column then
use this SQL
Else
use this SQL
End If

??

Here's the code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Command0_Click()
  3.  
  4.  
  5. On Error Resume Next
  6.  
  7. Dim dbs As Database
  8. Dim Rs As DAO.Recordset
  9. Dim a As String
  10. Dim b As String
  11. Dim strSQL As String
  12. Dim strQueryName As String
  13. Dim qryDef As QueryDef
  14.  
  15. Set dbs = CurrentDb
  16.  
  17. a = Me.Combo1
  18. b = Me.Text7
  19. strQueryName = "myQuery"
  20.  
  21.  
  22. dbs.QueryDefs.Delete strQueryName
  23.  
  24. If '**What do I put here?** Then
  25.     'lookup column
  26.     strSQL = "SELECT ProjectRegForm.*, ProjectRegForm." & a & " FROM ProjectRegForm WHERE (((ProjectRegForm." & a & ".Value)LIKE '*" & b & "*'))"
  27.  
  28.     Else
  29.     'not lookup column
  30.     strSQL = "SELECT ProjectRegForm.*, ProjectRegForm." & a & " FROM ProjectRegForm WHERE (((ProjectRegForm." & a & ")LIKE '*" & b & "*'));"
  31.  
  32. End If
  33.  
  34. Set qryDef = dbs.CreateQueryDef(strQueryName, strSQL)
  35.  
  36. DoCmd.OpenQuery "myQuery", acViewNormal
  37.  
  38. End Sub
  39.  
Thanks for having a look at this!

Cheers,
Jason
Jun 21 '10 #1
3 1925
MikeTheBike
639 Expert 512MB
Hi

I think it must boil down to the "Values" returned from the bound column of Combo1.

You may need to tweek the Control Source query for the combo list.

What are the different values returned from Combo1 for each case, and which work for which case?


MTB
Jun 22 '10 #2
Thanks for your reply, Mike.
Actually, I figured this out late last night.

What I did was, I added this to the declarations area:

Dim tdf as DAO.Tabledef
set tdf = Dbs.tabledefs(ProjectRegForm)

and then I used this as my if-then statement:
Expand|Select|Wrap|Line Numbers
  1. If tdf.Fields(a).Type = dbText Then
  2.  
  3.    'not lookup column
  4.     strSQL = "SELECT ProjectRegForm.*, ProjectRegForm." & a & " FROM ProjectRegForm WHERE (((ProjectRegForm." & a & ")LIKE '*" & b & "*'))"
  5.  
  6.     Else
  7.     'lookup column
  8.     strSQL = "SELECT ProjectRegForm.*, ProjectRegForm." & a & " FROM ProjectRegForm WHERE (((ProjectRegForm." & a & ".value)LIKE '*" & b & "*'));"
  9.  
  10. End If
  11.  
It checks to see if the field is regular text, so if it is a look-up column (complex text) it uses the Else SQL.

Now I can probably put in an else-if statement for dates and numbers, as well - not sure what I should change ".value" to for that, or if I even need to change it.

Thanks to anyone who had a look at this and tried to figure it out!

Cheers,
Jason
Jun 22 '10 #3
Oh, and I just realized that you should put the field name (variable "a") in square brackets. That solves the problem of field names with spaces in them.
Jun 22 '10 #4

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

Similar topics

1
by: Frederico Ottoni | last post by:
Hi, I have a table where there is a field that is a lookup column referencing data in another table. How can I access this lookup field data? Using rst!FieldName it gives me "item not found in...
7
by: Don Riesbeck Jr. | last post by:
I'm working on an application (OEM) using C# that utilizes input from a keyboard, and USB Barcode Scanner. The scanner is a HID Keyboard device, and input from it is sent to the system as if it...
8
by: Moondog | last post by:
I am referencing a datagrid item like this: strItemNo = grdItems.Item(grdItems.CurrentRowIndex(), 0) The Item Number is in the first column; column 0. It works fine except this is not a...
9
by: Leszek | last post by:
Hi. How can I get just one row from selected column and put it into html dropdown list I tried like this: function pobierz_wszystko($tabela,$kolumna) { $zapytanie="SELECT $kolumna FROM...
3
by: Greg | last post by:
My problem is that values in the bool column of a datagrid are only being updated to the database once the focus of the bool cell is lost. This is completely counter-intuitative. When a user clicks...
3
by: dei3cmix | last post by:
Hey, I am having a problem with a program I am working on. Basically, the first part of the program gets input from a file using cin.getline. Then the second part, (still in the same main as the...
1
by: redpayne | last post by:
Okay, I finally got this program to run according to what the book had us build it as. Now prof wants case 2 and case 3 to prompt again for input, check input to see if it is the correct type, then...
1
by: orenl | last post by:
I have a DataRow (from DataTable) and i want to get data from a column name that might not exist. public static object getObject(DataTable dataTable, int index, string columnName) { return...
0
by: lini | last post by:
Hello, I am writing some code in the scenario which can be described as follow: + program A which writes to standard output (e.g. cout >> whatever). + program B which has GUI and also listens to...
116
by: dmoran21 | last post by:
Hi All, I am working on a program to take input from a txt file, do some calculations, and then output the results to another txt file. The program that I've written compiles fine for me, however,...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...

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.