A Coding Question?

CassandraB

Registered User.
Local time
Today, 16:13
Joined
Feb 23, 2003
Messages
54
I have a form with a subform. When I add a record to the subform I have to scroll to the right to continue to add to the fields (because it's wide). After I click my save button the form clears so I can go to the next record to add - but my problem is that my subform clears but it is stuck at the far right so I have to scroll back to the left when I go to add my next record, I would like it to jump back to the far left so I can begin at the first field. Can anyone help me with the snippet of code I need to add to the 'on click' of the save record button?

Here is what I have:

Private Sub Command133_Click()
On Error GoTo Err_Command133_Click


DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

DoCmd.GoToRecord , , acNewRec

Exit_Command133_Click:
Exit Sub

Err_Command133_Click:
MsgBox Err.DESCRIPTION
Resume Exit_Command133_Click

End Sub

What and where do I add the snippet of code?
Thanks a million to anyone who can help!
 
Hi Cassandra,
try this code

Private Sub Command133_Click()
On Error GoTo Err_Command133_Click


DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

DoCmd.GoToRecord , , acNewRec

'this will set the focus on the 1st field in your subform
Forms!yourmainformname!yoursubformname!yoursubform1stfield.setfocus
'this will then set the focus on your 1st field in your mainform
me.your1stfieldname.setfocus

Exit_Command133_Click:
Exit Sub

Err_Command133_Click:
MsgBox Err.DESCRIPTION
Resume Exit_Command133_Click

End Sub


The following will do what you want.
Hope this helps

Andy
 
Thank you Andy, your a gem!
 
I tried what you suggested but I get the error 'MS Access can't find the field 'sbfmACTVHIST' referred to in your expression'. This is what I put as far as code goes

Forms!MAINTENANCE!sbfmACTVHIST!SIGN#.Setfocus
Me.MP.Setfocus

What did I do wrong???
 
One, you can't have a #. Do not use special characters in table, query, form, report, module, or control names. # is a reserved character as well.

Also, lose the DoMenuItem thing. There's no guarantee that the menu item you have in this version will be the same in future versions.

Use this instead:

DoCmd.RunCommand acCmdSaveRecord
 
Got rid of the # - and I still get the same error. Thanks for your patience.
 
Cassandra,

When you took out the #, did you take it out of the code and take the # out of the field name.

Andy
 
yes I changed it to serial_no in all places....., it keeps coming back with not being able to find the field 'sbfmACTVHIST' in the expression. sbfmACTVHIST is my subform.
 
I tried this on one of my databases witha subform and I have no problem.

forms![formname]![subformname]!fieldname.setfocus
forms![formname]!fieldname.setfocus

The only thing I can think of is that that is a reference to your subform amongst the code.

Can you post a db or has Rich's suggestion worked.

Andy
 
Make sure that you are referencing the name of the CONTROL that contains the subform on the main form and not the subform itself, unless they have the same name.
 
If you click on the 'maintenance form' tab on the 'table of contents' and go to the bottom of the form to the floppy disk icon that saves record, this is where I placed the code behind (on-click). I doubt I will be successful in sending the DB because it exceeds the size limitation but I will try anyway..... Thank you so very much for taking a look.
 
Here you go, Chaps. I've stripped it down to the bare necessities. Again, I appreciate your time and help.
 

Attachments

A couple of things:

One, you want to name your controls instead of leaving them as Text_Box24, etc. It will save you and anyone who may follow you the pain of trying to figure out what is what.

Second, you should never use reserved words for names of controls. You currently have a control named DATE and that is a reserved word. That will honk things up for you as well.

Next, you still had a control named Sign# using the character.

Next, don't name your controls the same name as your table column names, make them slightly different. The method of using things like txtMyTextBox or cmbMyComboBox will work. Otherwise certain formulas or other things will not work properly.

Also, you say you had the code behind a save record button, but there is no button that I can see.
 
In the maintenance form, at the bottom of the form, there isn't an icon that looks like a floppy disk? I attached it to that under events, on-click. (I'm at home now, so I can't check it) When I stripped the database down to make it smaller to send, maybe something happened to it. I'll check it in the morning.

So all those poorly chosen naming conventions I used is most likely the problem with the setfocus issue. I will go through my entire database and make all of the suggestions you gave. I hope that I don't trip myself up making all the changes because every change seems to effect anything/everything else that has something to do with it. (I stripped out a bunch of queries, reports, etc. ) I wish I had more 'know how' on how to troubleshoot, but I guess this is where I'm going to learn how to be better at it.

I appreciate your guidence, Bob, and will get to it first thing in the morning. Thanks again.
 
Checked out the attachment and everything seems to be there. I had to copy it to my desktop to have write access.................
 
This simple line of code got it to work.......

Forms![MAINTENANCE]![sbfmACTVHIST]!SignNo.SetFocus

Thanks for everyones help.:p
 

Users who are viewing this thread

Back
Top Bottom