473,480 Members | 2,840 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Disabling combo box changes after selection

28 New Member
Hello,

I have a combo box [PHIconsultantcombo] on a form that I would like to 'lock' the selection of after the user makes a selection, then either moves to the next record or saves the form. This selection will therefore be unable to be changed afterwards.

From my research, I'm assuming that the following code needs to be placed into form_current event somehow:
Expand|Select|Wrap|Line Numbers
  1. Private Sub PHIconsultantcombo_LostFocus()
  2. PHIconsultantcombo.Locked = (PHIconsultantcombo.Text <> "")
  3.  
  4. End Sub
My current form_current event code looks like this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2. Me.RecordsetClone.MoveLast
  3. Me.RecordsetClone.MoveFirst
  4.  
  5. If Me.Lead_State <> "WA" Then
  6. Me.Statewarning = Null
  7. Else
  8. Me.Statewarning = "Note: THC is not sold in WA"
  9. End If
  10.  
  11. If Me.RecordsetClone.RecordCount = CurrentRecord Then
  12.     Me.but_next.Enabled = False
  13.     Me.but_last.Enabled = False
  14.     Else
  15.     Me.but_next.Enabled = True
  16.     Me.but_last.Enabled = True
  17. End If
  18.  
  19. If CurrentRecord = 1 Then
  20.     Me.but_previous.Enabled = False
  21.     Me.but_first.Enabled = False
  22.     Else
  23.     Me.but_previous.Enabled = True
  24.     Me.but_first.Enabled = True
  25. End If
  26. End Sub
How do I add the lostfocus code to the current event form code? Or am I on completely the wrong track?
Jun 6 '08 #1
9 6406
LBryant
18 New Member
If it's a bound control, use AfterUpdate rather than LostFocus

Expand|Select|Wrap|Line Numbers
  1. Private Sub PHIconsultantcombo_AfterUpdate()
  2.      If PHIconsultantcombo.Text <> "" then = PHIconsultantcombo.Locked = True
  3. End Sub
  4.  
Would that work? It doesn't need to be inside the form_current event does it?
Jun 6 '08 #2
missinglinq
3,532 Recognized Expert Specialist
To begin with you need to use the .Value property, not the .Text property.Text is only valid when the control has focus.

And yes, it does have to be in the OnCurrent event! Anytime you set some kind of formatting or properties (such as colors, visibility, enabling) in the AfterUpdate event of a control, you also have to include the code in the form's OnCurrent event. Otherwise, if you move from a record where, for example, a combobox is enabled, to a record where it should be disabled, it will inappropriately be enabled in the second record!

Linq ;0)>
Jun 7 '08 #3
dozingquinn
28 New Member
Thanks missinglinq.

Just confirming - I therefore need to use the following code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub PHIconsultantcombo_LostFocus()
  2. PHIconsultantcombo.Locked = (PHIconsultantcombo.Value <> "")
  3.  
Since I'm an Access Newbie, how would I best incorporate this into my current event code shown in my question?

Thanks again.
Jun 10 '08 #4
missinglinq
3,532 Recognized Expert Specialist
I'd just place it as the first line, immediately following

Private Sub Form_Current()

Linq ;0)>
Jun 10 '08 #5
dozingquinn
28 New Member
Hi again,

I've now placed the code at the top of form_current. This has managed to prevent combo box changes being made to existing records. However when I go to create a new record I'm met with the error:

Run-time error 94: Invalid use of Null

I select debug and the 'PHIconsultantcombo.Locked' line is highlighted as being the section of code causing the problem.

Is there anyway to overcome this and ensure that any combobox value can be selected for a newly created record?

Thanks.

My new code is below


Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2. PHIconsultantcombo.Locked = (PHIconsultantcombo.Value <> "")
  3. Me.RecordsetClone.MoveLast
  4. Me.RecordsetClone.MoveFirst
  5.  
  6.  
  7. If Me.Lead_State <> "WA" Then
  8. Me.Statewarning = Null
  9. Else
  10. Me.Statewarning = "Note: THC is not sold in WA"
  11. End If
  12.  
  13. If Me.RecordsetClone.RecordCount = CurrentRecord Then
  14.     Me.but_next.Enabled = False
  15.     Me.but_last.Enabled = False
  16.     Else
  17.     Me.but_next.Enabled = True
  18.     Me.but_last.Enabled = True
  19. End If
  20.  
  21. If CurrentRecord = 1 Then
  22.     Me.but_previous.Enabled = False
  23.     Me.but_first.Enabled = False
  24.     Else
  25.     Me.but_previous.Enabled = True
  26.     Me.but_first.Enabled = True
  27. End If
  28. End Sub
  29.  
Jun 10 '08 #6
missinglinq
3,532 Recognized Expert Specialist
Change

Expand|Select|Wrap|Line Numbers
  1. PHIconsultantcombo.Locked = (PHIconsultantcombo.Value <> "")
  2.  
to

Expand|Select|Wrap|Line Numbers
  1. If Not Me.NewRecord Then
  2. PHIconsultantcombo.Locked = Not IsNull(PHIconsultantcombo.Value)
  3. Else
  4. PHIconsultantcombo.Locked = False
  5. End If
  6.  
Linq ;0)>
Jun 10 '08 #7
dozingquinn
28 New Member
Thanks so much for your help missinglinq,

Your solution works perfectly.
Jun 11 '08 #8
missinglinq
3,532 Recognized Expert Specialist
Glad we could help!

Linq ;0)>
Jun 11 '08 #9
NeoPa
32,556 Recognized Expert Moderator MVP
I like the way you assign a boolean result to the boolean attribute, and also that you noticed the use of the [ CODE ] tags after your first post and used them subsequently :)

For your current logic, a single line, similar to your first will work (although Linq's code works perfectly too of course).
Expand|Select|Wrap|Line Numbers
  1. Me.PHIconsultantcombo.Locked = _
  2.     Not (IsNull(Me.PHIconsultantcombo) Or Me.NewRecord)
I would question the logic of locking the ComboBox after updates (to the control) though, and suggest that it should (possibly - only you know if this makes better sense) lock it only for saved records (with a selected value obviously). To enable this logic, simply use the same code, but only in the OnCurrent event procedure (not in AfterUpdate).
Jun 12 '08 #10

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

Similar topics

3
384
by: Steve | last post by:
C# I have some combo boxes, full of lookup descriptions. When I retrieve a dataset for my record, the values that need binding to the combos are the actual record IDs that relate to these...
1
8316
by: Matt Evans | last post by:
Hi, this must have been asked for before, but I cannot see it. Hopefully this is very simple. I have a form which shows everything from a table. In this table is a picture field called photo....
1
2216
by: Maria Joao | last post by:
I have two synchronized combo boxes and after the selection of the desired record, I need the user to open the related report, by pressing a button. My problem is that a combo box doesn't update...
2
3107
by: Danny | last post by:
I have a combo box look up and list items in a table, it is not bound. It works fine but how can I prevent the users from entering in there own data? i have an 'on change' event that when it...
5
1801
by: Cillies | last post by:
Hi All, This message is a continuation of an earlier post, so please accept my apologies as I believe I would get a better response this way. Problem: I have 4 combo boxes which I want to...
1
6118
by: Will | last post by:
I have a combo box on a form which is based on table tblMachine. On that combo box I have four columns visible MachineNumber, description, location and type. The bound column is the MachineNumber...
4
7131
by: meganrobertson22 | last post by:
Hi Everyone- I have a question about how to add and then use the "All" selection in a combo box. I am trying to figure out how to: (1) add "All" as a selection to a combo box and then (2)...
6
4170
by: bammo | last post by:
MS Access 2003, Windows XP SP2, VBA I have a continuous form that allows edits and filters, but not deletions or additions. I filter the form based on combining selections the user makes in...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7060
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6760
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
5365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4501
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3004
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.