Detect which column (1 Viewer)

kirkm

Registered User.
Local time
Tomorrow, 02:46
Joined
Oct 30, 2008
Messages
1,257
I have a datasheet-view subform on my main Form.
On the main form I want a button to do something with the selected datasheet column.

How can my code detect which column is selected? That is, it's name been clicked and the whole column has changed colour.
 

kirkm

Registered User.
Local time
Tomorrow, 02:46
Joined
Oct 30, 2008
Messages
1,257
Forms![your form name].ActiveControl.Name gives the first column name, not the selected one.
Screen.ActiveControl.Name gives the name of the button on the main Form.

I didn't write this database and some of the things are a bit hard to follow, I'm not even sure what the Form Name is. The underlying source for it is "frmCDTracks" but the subform may have a different name. I'm not 100% sure...but I have seen "SubCDTracks" in design view somewhere, but can't find it now to confirm. (And using that with ActiveControl.Name gives an error). I'm using Access 2007 and not very familiar with UI.
 

Eugene-LS

Registered User.
Local time
Today, 17:46
Joined
Dec 7, 2018
Messages
481
Try that way, please.
Code:
Me![Your object subform name].Form.ActiveControl.Name
 
Last edited:

kirkm

Registered User.
Local time
Tomorrow, 02:46
Joined
Oct 30, 2008
Messages
1,257
Thanks for the suggestion... but it (as before) printed the 1st column name, not the active one. I wonder if I'm using the right name? If I goto design mode and click the wee square where the rulers join (on the left), in Property Sheet Selection Type: Subform/Subreport I see "subCDTracks". Seems to be ticked.. and has a drop with other controls from the Main Form.
 

Eugene-LS

Registered User.
Local time
Today, 17:46
Joined
Dec 7, 2018
Messages
481
... I wonder if I'm using the right name? If I goto design mode and click the wee square where the rulers join (on the left), in Property Sheet Selection Type: Subform/Subreport I see "subCDTracks". Seems to be ticked.. and has a drop with other controls from the Main Form.
See picture bellow :
 

Attachments

  • SS01.PNG
    SS01.PNG
    39.7 KB · Views: 41

June7

AWF VIP
Local time
Today, 06:46
Joined
Mar 9, 2014
Messages
5,466
Sorry, I missed that you want to reference a subform. Where do you want to run the code? Eugene's suggestion works for me.

objSubForm should replace "Your object sub form"

Post your code.
 
Last edited:

kirkm

Registered User.
Local time
Tomorrow, 02:46
Joined
Oct 30, 2008
Messages
1,257
I think I'm doing all the right things, but it just isn't working.
I want to run he code from a button on the Main Form, and only have this so far.

Code:
Private Sub btnTitleCase_Click()
    MsgBox Me!subCDTracks.Form.ActiveControl.Name
End Sub
And here's my image to equate with Eugene's.
Perhaps I should create a tiny cut down version of the db and let you's try ?

The msgbox shows the name of the first column in the datasheet, not the one I have selected.
 

Attachments

  • Imageaccess1.jpg
    Imageaccess1.jpg
    72.4 KB · Views: 38

Eugene-LS

Registered User.
Local time
Today, 17:46
Joined
Dec 7, 2018
Messages
481
I think I'm doing all the right things, but it just isn't working..
Well, let's try anoter way ...

Code:
Private sActiveControlName$

Private Sub btnTitleCase_Click()
    If sActiveControlName = "" Then subCDTracks_Exit False
        MsgBox "Active column:" & vbCrLf & sActiveControlName, vbInformation
End Sub

Private Sub subCDTracks_Exit(Cancel As Integer)
    sActiveControlName = Me!subCDTracks.Form.ActiveControl.Name
End Sub
I've tested - it works for me
 

kirkm

Registered User.
Local time
Tomorrow, 02:46
Joined
Oct 30, 2008
Messages
1,257
Your help is much appreciated. I'm getting the same result though so will attach the db.
 

Attachments

  • List.zip
    1,010.8 KB · Views: 38

kirkm

Registered User.
Local time
Tomorrow, 02:46
Joined
Oct 30, 2008
Messages
1,257
Ahh, hangon. It suddenly started working with Eugenes code. After I had exited and rebooted access. Hmmm sorry.. hate this sort of thing !
 

kirkm

Registered User.
Local time
Tomorrow, 02:46
Joined
Oct 30, 2008
Messages
1,257
Eugene, am I right in thinking you're forcing an exit from the subform if sActiveControlName is an empty string? And at that point it can somehow get the control name, but not otherwise ? Is there any reason for sending False ? I tried True and it seems the same.
Anyway what I want to do, now the column is known, is detect when its name is clicked and only then enable btnTitleCase. I have set sActiveControlName back to "" in the Forms Current event (else it remembers the last one) but if no column is selected it will return txtTTrack. That's not desirable as whatever sActiveControlName is will have stuff done to it, so should be selected first.
 

Users who are viewing this thread

Top Bottom