23. august 2010 - 13:40 Der er 10 kommentarer og
1 løsning

Access List events

Jeg har det problem, i Access 2007, at naar jeg i en formular laver en liste saa ruller vaerdierne i listen ofte ikke naar jeg traekker i schrollbaren.  Hvis jeg foerst traekker schrollbaren helt i bund en gang, saa ruller vaerdierne derefter.  Jeg blev raadet til at loese det ved i form_load koden at indsaette kode som:

With Me.mylist
.Selected(.ListCount - 1) = True
.Selected(0) = True
.Selected(0) = False
End With

Og det virker udemaerket.  Men derved fyres alle listens 'events' af.  Hvis jeg vil foretage noget efter at jeg har valgt en vaerdi i listen og proever med kode saa som:

Private Sub mylist_AfterUpdate()
[foretag en aktion]
End sub

saa foretages denne aktion foer jeg faar lejlighed til at vaelge en vaerdi.  Det har jeg indtil nu klaret ved at indsaette en knap til at starte aktionen.  Men det er mere klodset.

Kan nogen taenke paa en maade hvorpaa jeg baade kan faa listen til at rulle direkte efter at formularen er loaded og ogsaa bruge BeforeUpdate(), AfterUpdate(), OnClick(), o.s.v.

(Min Access installation er ikke paa dansk, saa jeg kender ikke de danske betegnelser, men jeg haaber det er forstaaeligt alligevel.)
Avatar billede terry Ekspert
23. august 2010 - 14:00 #1
Hi Christian
I seem to remember this problem being up before.

This is just a wild guess and I havent trie dit myself.
You could try moving to the last record in the list and then back to the first.
23. august 2010 - 16:41 #2
terry: yes, this problem was up in the tread http://www.eksperten.dk/spm/907000 .  I was sending the question in a bit of a rush, from Antwerp where I help in a welfare center, and when I said "jeg blev raadet til at loese det...." I was about to add "by terry," but I was not sure.

So I move to the last record in the list by selecting it in the form_load() event.  Then the scrollbar works.  But that triggers the lists' BeforeUpdate(), AfterUpdate(), Click(), Enter(), Exit(), GotFocus(), and LostFocus() events and a number of other events.  That has so far not been a problem because whenever I used a list to chose a value to perform an action with I used a button to trigger the action.  However, I happened to read a book over VBA and that it should be possible to trigger the action directly by chosing an item in the list.  Having written the code I was surprised and increasingly frustrated that the action was triggered already when the form opened, and using the last item on the list, long before I got a chance to chose an item myself.  Then it finally dawned on me that the events I were tryng to use are triggered when the form_load() event selects in the list.

That is why I raised a sort of dual question:  How to make the list reacts on the scrollbar and also to be able to use list events to trigger actions?

a.  Could there be another way to make the scrollbar work than select the last item and thereby use up the list events?

b.  Or is there a way to re-use the list events?  (This may be a stupid question, but this is where I am in Access.)
Avatar billede terry Ekspert
23. august 2010 - 17:54 #3
If I understand the problem correcctkly, the listbox events get fired on the form load and also when you choose entries in the list. You want to be able to have code in these events but the code should only execute when you make selections and not on the forms on load?
23. august 2010 - 18:03 #4
That's correct.  For example the listbox lists the companies in an adress list.  Presently I chose a company, and in the OnClick event on a button causes a form to be opened that give further details on and allows updates on that company.  I want to avoid the button and instead have the form opened directly by chosing the company on the list.
Avatar billede terry Ekspert
23. august 2010 - 18:48 #5
You say that the following events are fired

(BeforeUpdate(), AfterUpdate(), Click(), Enter(), Exit(), GotFocus(), and LostFocus() events and a number of other events)


but as far as I can see only these are.
enter
gotfocus
exit

Anyway and idea is to set a boolean to True in the forms on Load event just before the code in the on load event gets executed. And back to false after the code is executed.

Then in the events which get fired because of the code you have in the On load event and where you intend having your own code you test if the boolean is True and if so exit sub.

Hope you get the idea, if not I'll give an example.
23. august 2010 - 21:07 #6
Once again you came up with the idea that helped me.  Please create a "svar" for points.

But about events let me explain that I for test reasons created code for every possible event and in each event set a MsgBox with the name of the event  (a trick I learned in the book I came across.)  That is how I know which events were fired.

Anyway, you advised me to create a boolean, set it to true before the intended Form_load() code and false after and then in the event I wanted to use for the subsequent code to Exit Sub if the boolean is true.

I created, in the declaration section of the module (so that the variable could be used throughout the Subs) Private b As Boolean.  In Form_Load() I set b to true before and to false after the code, and in Keuzelijst0_AfterUp0date I wrote this code:

Dim i As Integer
    If b = True Then Exit Sub
    MsgBox "after update"
    i = Me.Keuzelijst0

What happened was that the code in Form_Load() did not fire the Keuzelijst0_AfterUpdate() event and when the form opened and I selected an item from the list it set the variable i to the index of the item I selected.

It is my plan, in the AfterUpdate event, to continue with code such as DoCommand.OpenForm "MyForm",, and use the selected value as filter for the the data in MyForm.  So one additional question (I am greedy) is this:  Is in your opinion the AfterUpdate event the best place to set this code?  Or should I use BeforeUpdate, OnClick, or one or the other events?
Avatar billede terry Ekspert
23. august 2010 - 21:55 #7
I normally use debug.print "Name of event goes here"

This writes "Name of event goes here" in the debug (Immediate)window (CTRL+G). Saves having to press OK in the msgbox.

I did this for
Private Sub lstPersoonZoeken_AfterUpdate()
Debug.Print "after update"
End Sub

Private Sub lstPersoonZoeken_BeforeUpdate(Cancel As Integer)
Debug.Print "before update"
End Sub

Private Sub lstPersoonZoeken_Enter()
Debug.Print "enter"
End Sub

Private Sub lstPersoonZoeken_Exit(Cancel As Integer)
Debug.Print "exit"
End Sub

Private Sub lstPersoonZoeken_GotFocus()
Debug.Print "gotfocus"
End Sub

but only got these events when runing code in OnLoad event.
enter
gotfocus
exit


Anyway as long as you check the boolean in those you use then thats fine.

No your not greedy Christian, in England they say "if you dont ask you dont get"

In my opinion I would use the Double click event. Using other events can lead to from being opened unnecessarily. Using the double click is more safe, its a bit like selecting an item then pressing a button. Just selecting an item may not mean Open another form.
Avatar billede terry Ekspert
23. august 2010 - 21:56 #8
and that was an answer
Avatar billede terry Ekspert
23. august 2010 - 21:57 #9
Using other events can lead to forms being opened unnecessarily.
24. august 2010 - 06:45 #10
And the event doubleclick is not influenced by the selections made in the form onload event so I can avoid inserting the boolean.  But thanks for giving me the idea, that shall come in handy some time.
Avatar billede terry Ekspert
24. august 2010 - 08:30 #11
selv tak
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester