Can't equate one column in an Access record with another column in the same record

43 Views Asked by At

I am working with Access version MS 365 Apps for enterprise (desktop version).

When I try to get a specific field value out of my table, I instead get the equivalent to an empty or null result.

I'm in the debug phase. I provide an abreviated example here so you can see the field is reading as Null (i.e., it is saying that my called combo box value is the ID value (it is supposed to be a name) and that my table has no entry in the "Initials" field, which is associated with that record):

Selected value is the ID number, not the LastName field value, and there seems to be no other fields

In my table "People", with the autogenerated ID included as a column, I have only 4 columns (fields) in my table:

[Design View]

 ID - AutoNumber
 LastName - Long Text
 Type - Long Text
 Initials - Long Text

My table has only 9 entries and looks like this:

[Datasheet View]

ID LastName Type Initials
1 LastName1 Ms1 LNI1
...
9 LastName9 Dr9 LNI9

I am creating panels using the table "People".

In a userform, I have unbound combo boxes as input on LastName: cmbo_LastName1 (Row Source "People", Row Source Type "Table/Query", Bound Column "1"), cmbo_LastName2 (same), cmbo_LastName3 (same).

In form view, when I use the combo boxes, I see the last names as being displayed when selected (so I know it is dipping correctly into my table). The ID value is not displayed.

I am also calling the "Initials" value from the "People" table into my code because my output (report) will include that information:

Dim selectedName As Variant
Dim P1Initials As Variant

' Get the selected name from the combo box
selectedName = Me.cmbo_LastName1.Value

' Lookup to get the corresponding initials
P1Initials = DLookup("Initials", "People", "LastName = '" & selectedName & "'")

' Check for Null before assigning
If Not IsNull(P1Initials) Then
    ' Convert to String if not Null
    P1Initials = CStr(P1Initials)
Else
    ' Handle the case when no matching record is found
    P1Initials = "N/A"
End If

' At this debug stage, output the result
MsgBox "Selected Name: " & selectedName & vbCrLf & _
       "Corresponding Initials: " & P1Initials

The above code gave me a Selected Name as the ID number, rather than the LastName value. I could only get the LastName value by forcing the code: "selectedName = Me.cmbo_LastName1.Column(1)" -- I'm unclear why it wasn't on column 2: i.e., column 1 should be ID, which would make LastName column 2.

When I tried, instead, P1Initials = Me.cmbo_LastName1.Column(3)" (thinking to be clever), that resulted in a null error (also on column 2 and column 4).

I have to be missing something simple.

I would have thought it would be easy to collect the value from one column in a record and equate it and spit out the value from another column in the same record, without directly having to collect the information in a combo box from the user.

I do not want to have a second combo box on "Initials" (because I'm actually going to be doing more with the information in the table "People" and I don't want to have to collect every little piece of information found in the table).

I have tried

  1. repairing the database
  2. recreating the table
  3. making sure the first column width is 0
  4. making sure the column count is 4

Edit In the end, learning that ID was column indexed as 0, I ended up brute forcing the issue:

NamePanel1 = Me.cmbo_LastName1.Column(1)                    NamePanel2 = Me.cmbo_LastName2.Column(1)
NamePanel3 = Me.cmbo_LastName3.Column(1)
P1Initials = Me.cmbo_LastName1.Column(3)
P2Initials = Me.cmbo_LastName2.Column(3)
P3Initials = Me.cmbo_LastName.Column(3)

I never found out why the LastName value kept reverting back to ID.

1

There are 1 best solutions below

2
kdlsmith On

In the end, learning that ID was column indexed as 0, I ended up brute forcing the issue:

NamePanel1 = Me.cmbo_LastName1.Column(1)
NamePanel2 = Me.cmbo_LastName2.Column(1)
NamePanel3 = Me.cmbo_LastName3.Column(1)
P1Initials = Me.cmbo_LastName1.Column(3)
P2Initials = Me.cmbo_LastName2.Column(3)
P3Initials = Me.cmbo_LastName.Column(3)

I never found out why the LastName value kept reverting back to ID.