Function to show the fields with Yes value in a table query

Is that Yes text or is it a Y/N field - aka Boolean, True/False, -1/0 ??
And the answer to that is? It matters.

Regardless, how much help is it to know that there is a yes in a field when you don't know which of the 7,000 records it is in?
Do you need to know what record contains the value or not?
If so, how is the record identified - perhaps the first field is the unique record ID?
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
And the answer to that is? It matters.


Do you need to know what record contains the value or not?
If so, how is the record identified - perhaps the first field is the unique record ID?
Is that Yes text or is it a Y/N field - aka Boolean, True/False, -1/0 ??
- Yes fields is a Text field,

Regardless, how much help is it to know that there is a yes in a field when you don't know which of the 7,000 records it is in?
- Yes, it could be that the Column has multiple Yes.

Do you need to know what record contains the value or not?
-No, it is dynamic records
If so, how is the record identified - perhaps the first field is the unique record ID?
- Yes there is a unique record

Sample data:

Name of Field_0 is a unique Key

Name of Field_0Name of Field_1Name of Field_2Name of Field_3Name of Field_4Name of Field_5Name of Field_6
Record_ID_1YesxxxYesxxxxxxxxx
Record_ID_47xxxxxxxxxxxxYesxxx
Record_ID_98YesxxxYesxxxxxxYes
Record_ID_451xxxxxxxxxYesxxxxxx
Record_ID_770Yesxxxxxxxxxxxxxxx
Record_ID_771Yesxxxxxxxxxxxxxxx
Record_ID_1000Yesxxxxxxxxxxxxxxx
Record_ID_1600Yesxxxxxxxxxxxxxxx
Record_ID_4000Yesxxxxxxxxxxxxxxx
Record_ID_1395xxxxxxYesXXXXXX
Record_ID_1396xxxxxxYesXXXXXX

Result in a table:

Field_NameUnique Key
Name of Field_1Record_ID_1
Record_ID_770
Record_ID_771
Record_ID_1000
Record_ID_1600
Record_ID_4000
Name of Field_4Record_ID_1600
Name of Field_3Record_ID_1393
Record_ID_1394
Record_ID_1395
Record_ID_1396
Name of Field_4Record_ID_451
Name of Field_5Record_ID_47
Name of Field_6Record_ID_98
 
Upvote 0
Do you need to know what record contains the value or not?
-No, it is dynamic records
I think you misunderstood that because you said no then showed which record has the yes value in it (e.g. Record_ID_1)
Getting late here so I will post what I have for you to play with.

I made this a function because it's easier to run from the immediate window. You can change it to a sub if you need to.
The table containing the YES as text is in tblYesValues - change table name in code to your table name.
The first field in your table of yes values must be a number that id's the record. Since it appears that it is not, this won't work as written. You will have to tweak or we'll deal with it tomorrow.

The table that will show which record id's in that table is tblYes. Create that with field names:
YesID - autonumber (included in case it's needed; best to have it IMO)
RecID - Long, no default value, not required, no index
FieldName - short text, not required, no index
Do that to test the code. Once you have it working, you can use your own target table and change the table and field names in the sql variable.

Suggest you just paste as function into a standard module. Type function name in immediate window and hit return. Then tweak/alter only after it is working as required. Don't run until you save the project but I would compile first after pasting.

VBA Code:
Function dhen21dx()

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim i As Integer
Dim sql As String

On Error GoTo errHandler
Set db = CurrentDb
Set rs = db.OpenRecordset("tblYesValues", dbOpenSnapshot)

rs.MoveFirst
If Not (rs.BOF And rs.EOF) Then
  db.Execute "DELETE * FROM tblYes", dbFailOnError
  Do Until rs.EOF
    For i = 1 To rs.Fields.Count - 1
      If rs.Fields(i) = "Yes" Then
        sql = "INSERT INTO tblYes (RecID, FieldName) VALUES (" & rs.Fields(0) & ",'" & rs.Fields(i).Name & "')"
        db.Execute sql, dbFailOnError
      End If
    Next i
    rs.MoveNext
  Loop
End If

exitHere:
On Error Resume Next
rs.Close
Set rs = Nothing
Set db = Nothing
Exit Function

errHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume exitHere
End Function
 
Last edited:
Upvote 0
Here's what my small sample results in when code is run
tblYes

YesIDRecIDFieldName
9​
1​
field1
10​
1​
field3
11​
2​
field2
12​
2​
field4
13​
3​
field3
14​
3​
field4
15​
3​
field5
YesID > perhaps useful, perhaps not. I would advise to have it.
RecID > the autonumber (unique id from the Yes records.
FieldName > the field name containing Yes in that record ID
 
Upvote 0
Here's what my small sample results in when code is run
tblYes

YesIDRecIDFieldName
9​
1​
field1
10​
1​
field3
11​
2​
field2
12​
2​
field4
13​
3​
field3
14​
3​
field4
15​
3​
field5
YesID > perhaps useful, perhaps not. I would advise to have it.
RecID > the autonumber (unique id from the Yes records.
FieldName > the field name containing Yes in that record ID
Hi Micron,

Yes it's working and i appreciate this function, i create autonumber on the table so can work. but can we make it string so i dont need to add field. I tried but i didnt get the right coding.
 
Upvote 0
You probably didn't insert the necessary quotes and/or brackets. Try changing to this line if you need to use your sample field names. If not, leave the test field names the same but include the red quotes.
"INSERT INTO tblYes (Field_Name, [Unique Key]) VALUES ('" & rs.Fields(0) & "','" & rs.Fields(i).Name & "')"
If that doesn't work, post what you tried.
 
Upvote 0

Forum statistics

Threads
1,221,525
Messages
6,160,328
Members
451,637
Latest member
hvp2262

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top