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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Previous Entry as Default 1

Status
Not open for further replies.

DLSE

Technical User
Sep 5, 2002
24
0
0
US
How do I setup a form to default to the most recently entered data? For example, in Field1 I enter "A", and in Field2 I enter "B". When I click the new entry button, I want "A" and "B" to appear as defaults for their respective fields. I would like to setup the entire form to do this, but can change individual fields if necessary. Also, is it possible to have one "new entry" button that presents a blank form & another button that presents a form with these defaults? Help with any part of my question will be greatly appreciated.
Thanks,
Matt
 
in the fields after update event
me.fieldname.defaultvalue = "'" & me.fieldname.value & "'"

if it is a date field replace the ' with a #

to present options there are several ways that come to mind
but

set a public variable

dim bolSetdefault as boolean

in the buttons
on click
set one to true and the other to false

private sub command1_Click
bolSetdefault = true
end sub

private sub command2_Click
bolSetdefault = False
end sub

then in the each fields code

if bolSetdefault = true then
me.fieldname.defaultvalue = "'" & me.fieldname.value & "'"
end if


 
Actually, I find it works better to do this in the form's AfterUpdate event. If you do it in the field's event, then once you've left the field the default is already changed. Then, if you hit the ESC key or Undo button, on a new record, the field won't revert to the value it had originally. If you do it in the form's AfterUpdate event, the defaults don't change until you're ready to enter the next record.

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
I entered this information in the "after update event" and then attempted to enter data. However, Access then says that it cannot find the macro "me." I am fairly new to Access and am not sure what steps need to be taken to correct this. I assume that a macro must be created. Any tips?
Thanks again,
Matt
 
Oops! gol4's answer is all about VBA code. He and I assumed you knew we meant for you to create an event procedure and put this code in it. Access professionals use VBA code almost exclusively, because macros can't deal with errors that occur and cause the user to see confusing messages.

To create an event procedure, set the event property in the form's property sheet to [Event Procedure], then click the "..." (Builder) button beside it. That will open a module and create an empty event procedure for you. You can then type or paste the code at the cursor location.

To use a macro to set a DefaultValue property, use the SetValue action. Set the Item property to [red]Forms![form name]![control name].DefaultValue[/red] and set the Expression property to [red]"'" & Forms![form name]![control name].Value & "'"[/red]. As gol4 explained, use the # characters instead of apostophes for a date.

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
RickSpr & Gol4-
Thanks for all the help! Entering the code using "event procedure" works great. However, when I attempt to enter a fieldname of two words, for example: "Intersection Name", there is an error in my code. Any thoughts?
Thanks,
Matt
 
See those [] brackets I've been using, such as [form name]? They're used around the name of an object (such as a table, field, query, form, control, report, macro, or module) when the name contains a space.

They can be used around an object name in any case, even when it doesn't contain a space, so lots of times our examples will include the brackets just in case you replace our example name with a name of your own, that has a space in it.

Gol4, in his example "me.fieldname.defaultvalue", did not include the brackets around [fieldname], but that's what you need.

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
I inserted the brackets around the field....thanks. However, when I use this form, it returns to Microsoft Visual Basic, highlights ".defaultvalue =", and says "Compile Error: Method or Data Member Not Found".
Sorry for all the questions, but I've only had a 2 month Access class in college & am trying to make a fairly complex database (for me at least) at a summer job. I think its finished once this problem is solved!
Thanks you very much for all your help,
Matt
 
Also, the field I am working with uses a combo box, just incase this makes any difference.
Thanks again,
Matt
 
ComboBox objects have a DefaultValue property.

Cut and paste the statement, or even better the whole procedure, indicating which line has the error.

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
I copied and pasted the whole thing...I think its what you wanted. The 2nd line of type (Private Sub Combo 56...ect) is highlighted in yellow with a yellow arrow pointing toward it from the left. Just beneath this, ".DefaultValue =" is highlighted in blue.


Option Compare Database

Private Sub Combo56_AfterUpdate()
Me.[Intersection Code].DefaultValue = "'" & Me.[Intersection Code].Value & "'"
End Sub

Private Sub Date_AfterUpdate()
Me.Date.DefaultValue = "#" & Me.Date.Value & "#"
End Sub

Private Sub First_Click()
On Error GoTo Err_First_Click


DoCmd.GoToRecord , , acFirst

Exit_First_Click:
Exit Sub

Err_First_Click:
MsgBox Err.Description
Resume Exit_First_Click

End Sub
Private Sub Previous_Click()
On Error GoTo Err_Previous_Click


DoCmd.GoToRecord , , acPrevious

Exit_Previous_Click:
Exit Sub

Err_Previous_Click:
MsgBox Err.Description
Resume Exit_Previous_Click

End Sub
Private Sub Next_Click()
On Error GoTo Err_Next_Click


DoCmd.GoToRecord , , acNext

Exit_Next_Click:
Exit Sub

Err_Next_Click:
MsgBox Err.Description
Resume Exit_Next_Click

End Sub
Private Sub Last_Click()
On Error GoTo Err_Last_Click


DoCmd.GoToRecord , , acLast

Exit_Last_Click:
Exit Sub

Err_Last_Click:
MsgBox Err.Description
Resume Exit_Last_Click

End Sub
Private Sub New_Click()
On Error GoTo Err_New_Click


DoCmd.GoToRecord , , acNewRec

Exit_New_Click:
Exit Sub

Err_New_Click:
MsgBox Err.Description
Resume Exit_New_Click

End Sub
Private Sub Find_Click()
On Error GoTo Err_Find_Click


Screen.PreviousControl.SetFocus
DoCmd.DoMenuItem acFormBar, acEditMenu, 10, , acMenuVer70

Exit_Find_Click:
Exit Sub

Err_Find_Click:
MsgBox Err.Description
Resume Exit_Find_Click

End Sub

Private Sub Remarks_AfterUpdate()
Me.Remarks.DefaultValue = "'" & Me.Remarks.Value & "'"
End Sub
 
Since the control is named Combo56, your statement should be
Me.[Combo56].DefaultValue = "'" & Me.[Combo56].Value & "'"

You know, these are really pretty basic mistakes we're helping you with. I think you need to get better grounded in the fundamentals, maybe take a class or read a tutorial book. Tek-Tips is here to help professional Access developers and users share solutions to difficult problems. It's not "Access Programming 101".

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top