Open form to specific record

J

JMorrell

I would like to be able to open a form (of records) to a specific record based on a WHERE condition in a . Have not been able to successfully launch the DoCmd.OpenForm action

The line reads
DoCmd.OpenForm "frmNewEmp", , strWhereCategory,
where strWhereCategory is: "EmployeeID = <a number>

The form opens up, but to the first record only. The number is in the underlying table that feeds the form frmNewEmp. Why does the form open to the first record and not to the one specified in the WHERE statement

tia
JMorrell
 
N

Niklas Östergren

Hi JMorrell!

You are close!


Her´s the code for it:
****************************************************************
Dim intYourVariableNameHoldingANumber as Integer

strWhereCategory = "[EmployeeID ]=" & intYourVariableNameHoldingANumber
DoCmd.OpenForm "frmNewEmp", acNormal, , strWhereCategory
****************************************************************

// Niklas


JMorrell said:
I would like to be able to open a form (of records) to a specific record
based on a WHERE condition in a . Have not been able to successfully launch
the DoCmd.OpenForm action.
The line reads:
DoCmd.OpenForm "frmNewEmp", , strWhereCategory,
where strWhereCategory is: "EmployeeID = <a number>"

The form opens up, but to the first record only. The number is in the
underlying table that feeds the form frmNewEmp. Why does the form open to
the first record and not to the one specified in the WHERE statement?
 
J

JMorrell

That's not quite right, yet. The form is based on a query and when the form opens, it still opens with the first record in the table. Should I be filling the form's query with the WHERE statement?

JMorrell
 
F

fredg

I would like to be able to open a form (of records) to a specific record based on a WHERE condition in a . Have not been able to successfully launch the DoCmd.OpenForm action.

The line reads:
DoCmd.OpenForm "frmNewEmp", , strWhereCategory,
where strWhereCategory is: "EmployeeID = <a number>"

The form opens up, but to the first record only. The number is in the underlying table that feeds the form frmNewEmp. Why does the form open to the first record and not to the one specified in the WHERE statement?

tia,
JMorrell

You've placed the criteria in the wrong argument position.
You need 3 commas, not 2.
DoCmd.OpenForm "frmNewEmp", , , strWhereCategory,
 
F

fredg

You've placed the criteria in the wrong argument position.
You need 3 commas, not 2.
DoCmd.OpenForm "frmNewEmp", , , strWhereCategory,

I forgot to remove your ending comma.
The previous post should have read:

You've placed the criteria in the wrong argument position.
You need 3 commas, not 2.
DoCmd.OpenForm "frmNewEmp", , , strWhereCategory
 
N

Niklas Östergren

OK!

What code do you have in the form´s open_event?
And in which event did you place this code?

Is the number you use in the criteria part of DoCmd.OpenForm relay a number
(Integer / Long) or is it a string?

As long as it´s realy of integer or long data the code should work!
Please post the complete code in the event in which you open up the form AND
the code that goes in the form´s open_event!

I live in Sweden and it´s quit late so I will not be able to take a look at
it untill tomorow (apr. 8 hour´s). So if anyone else might help you out
quicker it would be good. I´ll take a look in the fourm tomorrow anyway!

// Niklas



JMorrell said:
That's not quite right, yet. The form is based on a query and when the
form opens, it still opens with the first record in the table. Should I be
filling the form's query with the WHERE statement?
 
J

JMorrell

The form's open event is Form_Current() the very start of which is

Dim passid As Strin

passid = Forms![EmployeeDialogForm]![EmployeeID

where the previous form is EmployeeDialogForm
and the field I'm passing to the new field is a value from that form, not a "field" or "control".

In the Expression Watch area in debug mode, EmployeeID still has a value in the new form, so I believe that it's available to me. I just haven't gotten the syntax right yet. I should add that the passed value (EmployeeID) is a string type value

thanks again
JMorrell

----- Niklas Östergren wrote: ----

OK

What code do you have in the form´s open_event
And in which event did you place this code

Is the number you use in the criteria part of DoCmd.OpenForm relay a numbe
(Integer / Long) or is it a string

As long as it´s realy of integer or long data the code should work
Please post the complete code in the event in which you open up the form AN
the code that goes in the form´s open_event

I live in Sweden and it´s quit late so I will not be able to take a look a
it untill tomorow (apr. 8 hour´s). So if anyone else might help you ou
quicker it would be good. I´ll take a look in the fourm tomorrow anyway

// Nikla



JMorrell said:
That's not quite right, yet. The form is based on a query and when th
form opens, it still opens with the first record in the table. Should I b
filling the form's query with the WHERE statement
 
N

Niklas Östergren

Hmm!

I found one thing in the code I posted to you which wasn´t right. I didn´t
dim variable <strWhereCategory> at all. It should look like this (see below)
and goes in top of the fom´s open_event code module:
*************************
Dim strWhereCategory as String
*************************

Maby that might help you? If not please keep on reading:

I don´t quit follow you!

Is the complete code which goes in the forms open:__event (the form which
you try to open but have problem with) realy only:

*************************************************
Private Sub Form_Open(Cancel As Integer)

Dim passid As String
passid = Forms![EmployeeDialogForm]![EmployeeID]

End Sub
****************************************************

Question:
1.) What do you do with the value in variable <passid>???
1a) Is <passid> a name of a control in the form?

2.) If you do have more code in the form´s open_event accept the one above
please post it as well.

3.) How about the complete code from where you try to open up the form,
please post the complete code exactly the way it looks like.

The code to open up a form where you have an integer in criteria is:
****************************************************************
Dim intYourVariableNameHoldingANumber as Integer
Dim strWhereCategory as String

strWhereCategory = "[EmployeeID ]=" & intYourVariableNameHoldingANumber
DoCmd.OpenForm "frmNewEmp", acNormal, , strWhereCategory
****************************************************************

The code to use if you use a string variable is:
===========================================================
Dim strYourVariableNameHoldingANumber as Stringr
Dim strWhereCategory as String

strWhereCategory = "[EmployeeID ]= 'strYourVariableNameHoldingANumber'"
DoCmd.OpenForm "frmNewEmp", acNormal, , strWhereCategory
===========================================================

If this doesn´t help you please answer the Q above!

// Niklas


JMorrell said:
The form's open event is Form_Current() the very start of which is :

Dim passid As String

passid = Forms![EmployeeDialogForm]![EmployeeID]

where the previous form is EmployeeDialogForm,
and the field I'm passing to the new field is a value from that form, not a "field" or "control".

In the Expression Watch area in debug mode, EmployeeID still has a value
in the new form, so I believe that it's available to me. I just haven't
gotten the syntax right yet. I should add that the passed value
(EmployeeID) is a string type value.
 
J

JMorrell

Thanks for your attention to this problem of mine.

Here is the code of the first form
Private Sub Preview_Click(
On Error GoTo Err_Preview_Clic

Select Case Me!EmployeeToVie
Case
DoCmd.OpenForm "frmNewEmp
Case
Dim strWhereCategory As Strin
Dim strTemp As Strin
strTemp = Forms![EmployeeDialogForm]!SelectEmploye
EmployeeID = Left(strTemp, 5
strWhereCategory = "[EmployeeID]= " & EmployeeID & "
If IsNull(Forms![EmployeeDialogForm]!SelectEmployee) The
DoCmd.OpenForm "frmNewEmp
Els
DoCmd.OpenForm "frmNewEmp", , strWhereCategor
' DoCmd.OpenForm "frmNewEmp", , strWhereCategor
End I
End Selec
' DoCmd.Close acForm, "EmployeeDialogForm

Exit_Preview_Click
Exit Su

Err_Preview_Click
Resume Exit_Preview_Clic

End Su
++++++++++++++++++++++++++++++++++++++++
and the code for the second form (but for the sake of brevity, only the first sequence of events)
Private Sub Form_Current(

' *******************************************************
' ** look at tblLeaveBal to get latest leave balance
' *******************************************************
Dim sleavebal As Double, aleavebal As Double, cleavebal As Doubl
Dim aleaveuse As Double, sleaveuse As Double, cleaveuse As Doubl
Dim aleavetemp As Double, sleavetemp As Double, cleavetemp As Doubl
Dim aleavecalc As Double, sleavecalc As Double, cleavecalc As Doubl

Dim EmployeeID As Strin
Dim passid As Strin

passid = Forms![EmployeeDialogForm]![EmployeeID

' **********************
' capture annual leave number
' first look to see if the info came from somewhere els
If Not IsNull(EmployeeID) The
Me.boxSSN = EmployeeI
End I

On Error Resume Nex
aleavebal = DLookup("annbal", "tblLeaveBal", "ssn = '" & Me.boxSSN & "' "
If Err The
aleavebal =
Err.Clea
On Error GoTo
End I
On Error Resume Nex
aleaveuse = DLookup("LeaveHours", "tblEmpLeave", "empid = '" & Me.boxSSN & "' and leavetype = '" & "A" & "'"
If Err The
aleaveuse =
Err.Clea
On Error GoTo
End I
aleavecalc = aleavebal - aleaveus
 
N

Niklas Östergren

Me!EmployeeToView is a control on the current form, right!?
Can this control realy have value 1 or 2???

I think you are having problem with the Select Case stament here. And since
I don´t know for sure what you´r trying to do it´s a little bit difficult
for me to give you a code that work.

What I would do is to comment (') away the lines of code which has to do
with the Select Case statment and only keeping the code for opening the fom
with a filtered recordsource, like this (just to see if this code works
correct):

Click_event of a command button:
=====================================================
Private Sub Preview_Click()

Dim strWhereCategory As String
Dim strTemp As String

On Error GoTo Err_Preview_Click

strWhereCategory = "[EmployeeID]= " & EmployeeID & ""

DoCmd.OpenForm "frmNewEmp", , strWhereCategory

Exit_Preview_Click:
Exit Sub

Err_Preview_Click:
Resume Exit_Preview_Click

End Sub
====================================================

Start with this simple code to see if the form opens up with the selected
record as desired!

Build the code up step by step and test it to see if it works the way you
want or if you, by the hand, build in errors. Maby this is the way you
already have done but at least stry the code above to see if it works or
not. It should work and it shoyld upen up the correct record as well, if it
exist offcource.

But try with a record you know exist and then when it works add the code for
handling no records and so on.

// Niklas



JMorrell said:
Thanks for your attention to this problem of mine.

Here is the code of the first form:
Private Sub Preview_Click()
On Error GoTo Err_Preview_Click
'
Select Case Me!EmployeeToView
Case 1
DoCmd.OpenForm "frmNewEmp"
Case 2
Dim strWhereCategory As String
Dim strTemp As String
strTemp = Forms![EmployeeDialogForm]!SelectEmployee
EmployeeID = Left(strTemp, 5)
strWhereCategory = "[EmployeeID]= " & EmployeeID & ""
If IsNull(Forms![EmployeeDialogForm]!SelectEmployee) Then
DoCmd.OpenForm "frmNewEmp"
Else
DoCmd.OpenForm "frmNewEmp", , strWhereCategory
' DoCmd.OpenForm "frmNewEmp", , strWhereCategory
End If
End Select
' DoCmd.Close acForm, "EmployeeDialogForm"

Exit_Preview_Click:
Exit Sub

Err_Preview_Click:
Resume Exit_Preview_Click

End Sub
+++++++++++++++++++++++++++++++++++++++++
and the code for the second form (but for the sake of brevity, only the first sequence of events):
Private Sub Form_Current()
'
' ********************************************************
' ** look at tblLeaveBal to get latest leave balances
' ********************************************************
Dim sleavebal As Double, aleavebal As Double, cleavebal As Double
Dim aleaveuse As Double, sleaveuse As Double, cleaveuse As Double
Dim aleavetemp As Double, sleavetemp As Double, cleavetemp As Double
Dim aleavecalc As Double, sleavecalc As Double, cleavecalc As Double

Dim EmployeeID As String
Dim passid As String

passid = Forms![EmployeeDialogForm]![EmployeeID]

' ***********************
' capture annual leave numbers
' first look to see if the info came from somewhere else
If Not IsNull(EmployeeID) Then
Me.boxSSN = EmployeeID
End If


On Error Resume Next
aleavebal = DLookup("annbal", "tblLeaveBal", "ssn = '" & Me.boxSSN & "' ")
If Err Then
aleavebal = 0
Err.Clear
On Error GoTo 0
End If
On Error Resume Next
aleaveuse = DLookup("LeaveHours", "tblEmpLeave", "empid = '" & Me.boxSSN
& "' and leavetype = '" & "A" & "'")
If Err Then
aleaveuse = 0
Err.Clear
On Error GoTo 0
End If
aleavecalc = aleavebal - aleaveuse
.
.
.
' pass it all on to the form
Me!boxAnnBal = aleavecalc
Me!boxsickbal = sleavecalc
Me!boxcompbal = cleavecalc

End Sub
++++++++++++++++++++++++++++

This is just an addition to the db that I thought would help the user find
a record on the form without having to scroll through the entire record
base. I'm beginning to wonder if it's worth it. btw, I'll be out of town
for the next 2 weeks (going to visit friends in Deutschland). I'll be
checking this site throughout the morning, so maybe I'll see your reply. If
not, I'll get it when I get back.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top