Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable a field on a form after data entered in that field

Status
Not open for further replies.

torf66

Programmer
Jun 10, 2003
43
US
I have a form MyForm that has a field on the form called
numAdmitNumber. I want it so that the user can only enter into this field when adding a new record to the database.
When they first come into the MyForm it comes to the first record in the database. I want the field numAdmitNumber to be visible but I do not want them to be able to update that field. They can update any of the other fields on the form but just not that one once the initial value is entered. This field numAdmitNumber is the key for the table of information being displayed. How can this be done?


Also is there a way when you are setting up a query in the criteria field to enter a wild card to a field defined as a number. For example I have a field called numAdmitNumber. When I run the query I want to be able to allow the user to enter any number into the entry and find the value. For example I enter a 1 then it finds any records that start with a 1 so I would fine admit numbers 12, 123 or 1234. If I enter 123 then I find the records 123, 1234.

Thanks,
Todd ttorfin@dakcl.com
 
Try
1. in the On Current Event
if not isnull(numAdmitNumber) then
numAdmitNumber.locked = true
end if

2. try Like

Hope this helps
Hymn
 
I have Access 97 I do not see the On Current Event would this be different in Access 97? Also I need the user to be able to when they want to add a record to be able to enter into this field. I only want them to be able to not enter into this field if there is a value already in this field when then edit a record.
 
go into design view of your form
go to properties/events/on current

if (not is null)means that if there is data in the field then the field will be locked. if no data then you can input data. if you require the field to locked as soon as data is inputted then place the code in the fields After Update Event as well

Hope this helps
Hymn
 
This will not allow me to input anything into the admit number when I try to enter a new record.
On the form properties under On Current I have this code
in

Private Sub Form_Current()

if not isnull(numAdmitNumber) then
numAdmitNumber.locked = true
end if

End Sub

It locks the field when there is something already in the
field but when I try to add a new record I cannot input anything into this field. This field is the key of the table. If you know what might be the problem let me know.
Thanks,
Todd
 
2. If I have Like[Enter an Admit Number] what do I need to put after the last bracket?
 
torf66

Hymm is absolutely correct. But you have to be editing the properties for the form, not a field.

As per your question regarding your query using wild cards with numbers, I think the coding would get a little messy ... 1 or 10 to 19 or 100 to 199, etc.

As an alternative, consider switching the numeric number to a text field. You can force the field to accept only numbers by using the input mask for the field to simulate a numeric filed from the user's perspective. I would even go so far to consider padding the number with leading or trailing 0's to achieve a fixed length.

This database change may be a little too extreme for what you wanted but it is the only simple way I know to easily add the functionality you are looking for.

Good luck.
Richard
 
I got question 2. to work I used
Like[Enter Admit Number]&"*"


I cannot get this logic to work.
if not isnull(numAdmitNumber) then
numAdmitNumber.locked = true
end if

I am doing this on the Form under Properties and using
the Code Builder. Should I be using the Expression Builder
or a Macro? Please help not sure what to do.
Todd
 
I slightly disagree with using the Current event here, as it fires every time you move from one record to another, which is not really necessary.
I'd use the GotFocus event of the text box:

Private Sub numAdmitNumber_GotFocus()
numAdmitNumber.Locked= Not IsNull(numAdmitNumber)
End Sub

This only checks the locked status of the text box when it gets the focus (possible editing attempt)

With the form in Design View, select the text box.
Go to View-Code
From the upper right combo box, select GotFocus
Paste the line:
numAdmitNumber.Locked= Not IsNull(numAdmitNumber)
Compile and open the form to test it. If it's OK, save it.

Good luck



[pipe]
Daniel Vlas
Systems Consultant

 
Todd
Where does this information in this field[numAdmitNumber]come from, is it automaticly inputted or user inputted.

I'm assuming from your post that it is user inputted.

if the field has no data you should be able to input data but once their is data in the field the field is locked and no data can be entered is the logic behind that piece of code.

You are doing it in the correct place Code builder

Hope this helps
Hymn
 
Todd
another thought! do you have a default value set in the form properties or in the table wich contains that field? if yes then "numAdmitNumber" will be always locked

Hope this helps
Hymn
 
Todd,

Regarding allowing entry when new but not allowing editing,
I'd use the following:

If Me.NewRecord Then
Me!PROPERTY.Locked = False
Me!PROPERTY.Enabled = True
Else
Me!PROPERTY.Locked = True
Me!PROPERTY.Enabled = False
End If

HTH,
Bob
 
This actually worked
Private Sub numAdmitNumber_GotFocus()
numAdmitNumber.Locked= Not IsNull(numAdmitNumber)
End Sub

Thank you Dan Vlas

I have one more question. How can I make the field dim out
when it is displayed or should I say grayed out so that it appears as though nothing can be entered into this field?
Is there a way if a user tries to enter something in this field that a message can be displayed some thing like
'Field Locked' or 'Field Cannot be Updated'

 
Set both enabled and locked to false and the control will be dimmed. When enabled is false, the control cannot be selected so there's no need for a message.
 
Use the KeyDown event for the text box to get a custom message on editing attempts:

Private Sub numAdmitNumber_KeyDown(KeyCode As Integer, Shift As Integer)
If numAdmitNumber.Locked Then
MsgBox "No changes possible"
End If

As for the colors, here is where you need the Current event:

Private Sub Form_Current()
If numAdmitNumber.Locked then
numAdmitNumber.BackColor = 255
numAdmitNumber.ForeColor = 65536
Else
numAdmitNumber.BackColor = 1672553
numAdmitNumber.ForeColor = 0
End if
End Sub

I have no clue what colors will be displayed with those figures (255 is red and 0 is black). It's up to you to find out the combination.

Disabling the textbox will prevent you from searching the field, so don't.

Good luck,



[pipe]
Daniel Vlas
Systems Consultant

 
One more question I have is when I add a new record and I key something into the numAdmitNumber and then I tab to the next field, if for example I entered 123 and when I got to the next field I realized my entry in numAdmitNumber should have been 1234 if I try to go back to that field and enter 1234 it will not allow me to enter anything into the field. Is there a way to edit this field before I have left the record I am adding to but even after I have entered something into this field say if I made a mistake in keying and realized it before I have moved onto adding a new record??
Thanks,
Todd
 
Is there a way to only have this message display when they try to enter something into the numAdmitNumber? When I tab or hit enter to go to the next field on the form the message "No Changes Possible" displays. Which they have not really entered anything new into the field but they are just tabbing or entering down to the next field on the form.

This is what I have in the On Key Down event procedure:

Private Sub numAdmitNumber_KeyDown(KeyCode As Integer, Shift As Integer)
If numAdmitNumber.Locked Then
MsgBox "No changes possible"
End If

Also when I first come into the form the field for numAdmitNumber is now greyed out but when I tab down or enter down to the next field the colors go away. Is there a way to keep the colors gray for the field numAdmitNumber no matter what field I am on in the form. The Grey background for the field numAdmitNumber only displays when I click back on the field. I want it to stay grey all the time when there is a value in the field. Another issue is when I go to the next record and that record displays the field numAdmitNumber is not grayed out even though there is a value in the field.
Thanks,
Todd

 
Just wondering if anyone has had a chance to look at my last question.

Thanks,
Todd
 
1. KeyDown is triggered whenever you hit a key (any key)
To limit it just to 'printable' characters, use the KeyCode argument:

Private Sub numAdmitNumber_KeyDown(KeyCode As Integer, Shift As Integer)
If (Not IsNull(numAdmitNumber)) And (KeyCode >= 32)) Then
MsgBox "No changes possible"
End If
End Sub

2. Don't check for the Locked property in the OnCurrent event procedure. Instead, check the return value of IsNull(control) function.

3. IMO you have enough information in this thread to do whatever you need to do with that control on the form. Read it carefully from the beginning and you'll see I'm right.

"God gives you the food, but doesn't chew it for you"


[pipe]
Daniel Vlas
Systems Consultant

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top