this code makes my access stop working!

behedwin

Active Member
Joined
Dec 10, 2014
Messages
399
Every time i run below code and the If statement is true, my access freezes up.

Code:
'count characthers in textbox
If Len(Me.txtpersonnummer.Text) = 10 Then
Me.WarningPersonnummer.Visible = False
Me.txtpersonnummer.BackColor = vbWhite
Exit Sub
Else
Me.WarningPersonnummer.Visible = True
Me.txtpersonnummer.BackColor = vbBlue
End If

If i remove so i have 9 numbers in the textbox called txtpersonnummer.
Then it all works.
But if i then put back 10 charachters in the textbox. My access locks up and i need to restart it.

Anyone got any idea of why?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Although there might be a reasonable explanation, I can't think of it. What kind of 9 or 10 digit numbers are you using? Are the fields bound or unbound? What exactly is Me.WarningPersonnummer and Me.txtpersonnummer?
 
Upvote 0
Me.WarningPersonnummer is a label that have a few charachters that is supposed to be shown as a warning.

Me.txtpersonnummer is a textbox holding the data.

The data is only numbers.
For example: 6505041238

So if the user types only 9 numbers or types 11 numbers... i want the textbox to change colour and display the label warning.

Me.txtpersonnummer is bound to a table with 3 columns.

Me.warningpersonnummer is not bound.
 
Last edited:
Upvote 0
Where is the code?
Code:
'count characthers in textbox
If Len(Me.txtpersonnummer.Text) = 10 Then
Me.WarningPersonnummer.Visible = False
Me.txtpersonnummer.BackColor = vbWhite
Exit Sub
Else
Me.WarningPersonnummer.Visible = True
Me.txtpersonnummer.BackColor = vbBlue
End If
 
Upvote 0
Step through the code, line by line, when there are 10 characters and watch the progression. We have no idea what precedes or follows this code. Perhaps execution leaves this sub (because of Exit Sub) and returns to finish somewhere else, and the problem is there.

Do you have Option Explicit 'turned on' and does your project compile without error?
P.S. - likely irrelevant, but are you sure you want to refer to the textbox's text property? That isn't exactly the same as it's Value property, to which you don't have to explicitly use anyway.
 
Upvote 0
The code is placed in a textbox after update event.
so when the textbox txtpersonnummer is updated, then this code runs.

full sub looks like this
Code:
Option Explicit
Private Sub txtpersonnummer_AfterUpdate()
Application.Echo False
Me.Painting = False

'count characthers in textbox
If Len(Me.txtpersonnummer.Text) = 10 Then
Me.WarningPersonnummer.Visible = False
Me.txtpersonnummer.BackColor = vbWhite
Exit Sub
Else
Me.WarningPersonnummer.Visible = True
Me.txtpersonnummer.BackColor = vbBlue
End If


'calculate birthdays
BirthdayDate
GetBirthdayAge

'display picture in profile based on personnummer
SexPicture

Application.Echo True
Me.Painting = True
End Sub

tried to step through the code, but cant see the error.
 
Upvote 0
It's difficult to see how this is killing access but you can easily just comment out the code:
Code:
'If Len(Me.txtpersonnummer.Text) = 10 Then
'Me.WarningPersonnummer.Visible = False
'Me.txtpersonnummer.BackColor = vbWhite
'Exit Sub
'Else
'Me.WarningPersonnummer.Visible = True
'Me.txtpersonnummer.BackColor = vbBlue
'End If

Make sure everything else is fine apart from this. Then come back to it later if you can verify that everything works fine without this validation.
 
Upvote 0
Use Before Update. After is too late to prevent anything or maybe even correct it.
What error? You didn't mention one before. Are you saying that you can step through this from one end to the other? Or Access hangs one the same line? Or do you get past it and it hangs in one of the other functions that you seem to be calling. It would help a lot if you would be much more informative.
 
Upvote 0
Here a testfile.

https://files.fm/f/cswbza2k

Some things might look strange in it... but go to to form Personal_Form
Go to the textbox with the text PERSONNUMMER above it.
change it to 9 or 11 numbers. exit the field and it should change colour.
enter back into the textbox and make it 10 numbers.
then it should change back to white colour... here access freezes for me.


I have now also tested switching to before update function of the textbox.
This does not change the problem for me.

I have now also tested commenting out the whole section, ie removing the code that update the colour and hides the label.
Then when i do the same thing, change the textbox personnummer to 9 or 11 numbers and then change it back to 10. My access does not freezes up. So i assume this is because i removed the code ie removing the problem.... so something in that code makes my access stop working.
 
Upvote 0
maybe you need this change(?):

Code:
If Len(Me.txtpersonnummer.Text) = 10 Then
    Me.WarningPersonnummer.Visible = False
    Me.txtpersonnummer.BackColor = vbWhite
    [B][COLOR="#FF0000"][SIZE=3]Application.Echo True
    Me.Painting = True
    [/SIZE][/COLOR][/B]Exit Sub
Else
    Me.WarningPersonnummer.Visible = True
    Me.txtpersonnummer.BackColor = vbBlue
End If

I'm not sure why you are changing those values but possible if painting means updating the screen and it's changed to false then you would appear to have a frozen access.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,237
Members
448,555
Latest member
RobertJones1986

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top