VBA error with incomplete textbox (1 Viewer)

Monardo

Registered User.
Local time
Today, 18:06
Joined
Mar 14, 2008
Messages
70
Hello,

I have a form with 3 textboxes (itemCode , txt1, txt2) and 1 button (simplifying for this example). The textbox itemCode has an input mask.
User must: 1) manually enter itemCode , 2) click the button and vba script fills txt1 and sets focus to txt2.

Code:
Private Sub button1_Click()
    Me.txt1 = "A10"
    Me.txt2.SetFocus
End Sub

Also, all three textboxes are unbound.

All works fine if after entering itemCode user hits Tab or Enter to move to next textbox, but if "text cursor" remains in the itemCode (at the end of entered value) I get an error

Run-time error 2110: Access can't move the focus to the control txt2

and on second try

Run-time error 7: Out of memory.

It must be something to do with textbox being "dirty", but can't find solution.

Edits: Added VBA code and changed textbox name, to be less confusing
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:06
Joined
Feb 19, 2002
Messages
42,970
Seems convoluted. Why are you duplicating data? Isn't having the data in the PK sufficient? Why do you need a button to cause anything to happen? What happens if the user closes the form and doesn't press the button?

Wouldn't putting the code in the first control's AfterUpdate event work? Then the user can make it happen with a tab or by clicking into the third control. You can make the tab order skip over the second control since you are not entering data into it anyway so it should actually be locked.

You've simplified the example to the point where it doesn't make sense.

It would also have been helpful if you had posted the code that raised the error. We can't debug code we can't even see.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:06
Joined
Feb 28, 2001
Messages
26,999
The first and most prominent cause of run-time error 7 is having too many programs running at once such that you run out of system-wide virtual memory. A possible error has to do with having too many files open, or having too large of a file open, or too many large files open. (That one gets complex because it involves multiple contributors). It can also be caused by having a program segment (a logical sub-division of memory) greater than 64KB which could occur with unusually large memory arrays in some module. Do ANY of the above apply to what you are doing?
 

Monardo

Registered User.
Local time
Today, 18:06
Joined
Mar 14, 2008
Messages
70
Seems convoluted. Why are you duplicating data? Isn't having the data in the PK sufficient? Why do you need a button to cause anything to happen? What happens if the user closes the form and doesn't press the button?

Wouldn't putting the code in the first control's AfterUpdate event work? Then the user can make it happen with a tab or by clicking into the third control. You can make the tab order skip over the second control since you are not entering data into it anyway so it should actually be locked.

You've simplified the example to the point where it doesn't make sense.

It would also have been helpful if you had posted the code that raised the error. We can't debug code we can't even see.

Sorry if I was unclear, but I don't think it's convoluted at all. I added the code in the original post as requested.
I am not duplicating any data. As all textboxes are unbound, nothing will happen if user closes form in the middle of entry, as intended.

To un-simplify, instead of 1 button there are 100 buttons on the form, which represent spatial distribution of cells in the storage box, but every button has exactly the same code as above, just different value for txt1, which represents coordinate in the box.
 

Monardo

Registered User.
Local time
Today, 18:06
Joined
Mar 14, 2008
Messages
70
The first and most prominent cause of run-time error 7 is having too many programs running at once such that you run out of system-wide virtual memory. A possible error has to do with having too many files open, or having too large of a file open, or too many large files open. (That one gets complex because it involves multiple contributors). It can also be caused by having a program segment (a logical sub-division of memory) greater than 64KB which could occur with unusually large memory arrays in some module. Do ANY of the above apply to what you are doing?
I don't think so. First of all the code is as simple as it could be (I added VBA in the original post). Secondly, it's just one form with 4 unbound textboxes (I just mentioned 3 in the example). And thirdly, the code works if editing/entering of first textbox is finished, but comes up only if user forgets to press Enter or Tab.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:06
Joined
Oct 29, 2018
Messages
21,358
Hi. Do you have any code assigned to the ItemCode Textbox?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:06
Joined
Feb 28, 2001
Messages
26,999
I don't think so. First of all the code is as simple as it could be (I added VBA in the original post). Secondly, it's just one form with 4 unbound textboxes (I just mentioned 3 in the example). And thirdly, the code works if editing/entering of first textbox is finished, but comes up only if user forgets to press Enter or Tab.
This cannot happen without something else going on. The code you showed us does not affect memory usage significantly. To get an error 7, you are talking about affecting many hundreds of MB or even a couple of GB of memory. That code you showed us MIGHT affect two or three bytes. We do not have all of the picture here.
 

onur_can

Active member
Local time
Today, 08:06
Joined
Oct 4, 2015
Messages
180
Dear The_Doc_Man, you have made a very good explanation.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:06
Joined
Feb 19, 2002
Messages
42,970
100 buttons with identical code? Why not create a table with the variables and use a combo to pick one? One control, one procedure, no conversion code.

So does your form have 300 controls? three for each or is it 100 buttons plus two target fields? Could be all the controls on the form causing a problem.
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:06
Joined
Feb 28, 2001
Messages
26,999
Having 100 buttons, each of which has two lines of code that assert a value and then try to set focus, won't cause the "out of memory" problem. Two lines of code plus two lines of declaration syntax for each sub x 100 buttons is still only 400 lines of code. It is highly unlikely that you would reach 64K lines that way. (Limits are 64K Lines/module x 1K characters/line x 1000 modules/db and separately, a little over 6600 lines/procedure.) You are NOWHERE near any of those limits.

Since an event cannot interrupt another event, you could click every button in rapid sequence and, even if there is a long-running subroutine in each event sub that would LET you click faster than the code could manage, all that would happen is that the event would be queued up for execution as the currently active routine does an Exit Sub or equivalent. Event code is linearized. At most, you would have one stack frame per event and in the worst case, one more stack frame for error trap code (if you had an error event routine declared - which you don't). Therefore, stack space can't be depleted by having so many buttons.

That long-winded discussion was leading up to this point: The memory problem has to be separate from the number of controls UNLESS there is code corruption. With corruption, all bets are off. To resolve this, try decompiling and recompiling the database.


If you choose to do this, make a copy of the DB FIRST, put it in another folder. That way if something goes haywire you have a copy as backup. Do the Decompile, then enter your VBA screen and do a Compile, then from the File ribbon, do a Compact & Repair. Don't get rid of that backup copy until this works better.

The inability to set focus COULD be caused by having set the properties of Me.Txt2 to not allow it to be tabbed, or by locking it. You must have the ability to tab to the control in order to set its focus. If it is blocked from normal use, that could cause your .SetFocus problem. If you have an issue with corruption, that could also affect your ability to set focus.
 

Users who are viewing this thread

Top Bottom