Error 2158 on a Recordset

Celestz

Registered User.
Local time
Today, 23:30
Joined
Feb 8, 2010
Messages
12
Hello, I'm new in these forums but and i'm fairly new at MS Access programming since I utilize java most of the time.

I'm currently using Access 2003 and was wondering why I encounter an error with my Code.

The Code is Below

Private Sub cmdSave_Click()
' Dim thisText As String
' thisText = DMax("ASRNUM", "ASR")
' MsgBox thisText
Dim db As DAO.Database, checkASR As DAO.Recordset, checkEntry As DAO.Recordset
Set db = CurrentDb
If MsgBox("Save records now?", vbYesNo, "Save ASR") = vbYes Then
Set checkASR = db.OpenRecordset("SELECT DateCoveredFrom,DateCoveredTo FROM ASR WHERE EmpID = " & TxtEmpID.Text)
checkASR.Requery
checkASR.MoveFirst
Do While Not checkASR.EOF
MsgBox checkASR("DateCoveredFrom")
checkASR.MoveNext
Loop
checkASR.MoveFirst
End If
End Sub

Error 2158 states that checkASR object does not have focus. Any idea how I can go around this?

Much thanks for the help since I'm really crawling in the dark.
 
Last edited:
Nvm guys, I found the error, my bad coz I didn't know you can access a control value using Me!ControlName.

Close this. Thanks.
 
Hi

I'll give a bit of Access background for you then. :-)

The controls in Access are different objects to those found in other environments (for example they're heavily data oriented, supporting data based events and binding and only become an active Window when they have the focus) and so they expose different properties, or at least expose some properties differently.
The Text property is only available when a control has the focus. It returns the value currently displayed in that control. The committed content of a control is exposed through its Value property, which can be accessed at any time while a form is open.
(The Value property is the default property of a control, and so you'll often see it ommitted in such code).
So your code would be:
...WHERE EmpID = " & TxtEmpID.Value)
or just
...WHERE EmpID = " & TxtEmpID)

I know you're likely just doing some testing at the moment, but just to mention that your immediate
checkASR.Requery
is redundant as you've only just opened the recordset. It already holds the latest set of records that it should. (A Requery is a fairly hefty overhead, analogous to re-opening the recordset as far as the database engine is concerned).

Cheers.
 
Hi

I'll give a bit of Access background for you then. :-)

The controls in Access are different objects to those found in other environments (for example they're heavily data oriented, supporting data based events and binding and only become an active Window when they have the focus) and so they expose different properties, or at least expose some properties differently.
The Text property is only available when a control has the focus. It returns the value currently displayed in that control. The committed content of a control is exposed through its Value property, which can be accessed at any time while a form is open.
(The Value property is the default property of a control, and so you'll often see it ommitted in such code).
So your code would be:
...WHERE EmpID = " & TxtEmpID.Value)
or just
...WHERE EmpID = " & TxtEmpID)

I know you're likely just doing some testing at the moment, but just to mention that your immediate
checkASR.Requery
is redundant as you've only just opened the recordset. It already holds the latest set of records that it should. (A Requery is a fairly hefty overhead, analogous to re-opening the recordset as far as the database engine is concerned).

Cheers.

Thanks for this informative post, I was really crawling in the dark until I read some posts here in the forums and realized what I was doing wrong.

I'll post again in the respective sub-forum if I encounter another big wall for my program.

Also thanks for telling me about that requery, I wasn't sure about that line since I read some that had requery in it.
 
That light blue colour you used in you posts is nearly unreadable at best. A receipe for eyestrain at worst.
 
That light blue colour you used in you posts is nearly unreadable at best. A receipe for eyestrain at worst.

Sorry, I got used to Cyan since the game forum I always frequent had dark colors. I won't let it happen again while here though, I also received some eyestrain.
 

Users who are viewing this thread

Back
Top Bottom