By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,509 Members | 1,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,509 IT Pros & Developers. It's quick & easy.

VBA code for adding, deleting and searching for a record.

P: n/a
I am a beginner to the more technical aspects of Microsoft Access
(2000) and was wondering if any one can help. I have 3 buttons on a
form which add, delete and search for a record. However, when I click
on the respective buttons absolutely nothing happens! I was wondering
if anyone could help? The code I have is below...

ADDING A RECORD

Private Sub cmdaddstudent_Click()
On Error GoTo Err_cmdaddstudent_Click
DoCmd.GoToRecord , , acNewRec

Exit_cmdaddstudent_Click:
Exit Sub

Err_cmdaddstudent_Click:
MsgBox Err.Description
Resume Exit_cmdaddstudent_Click

End Sub
DELETING A RECORD

rivate Sub cmddeletestudent_Click()
On Error GoTo Err_cmddeletestudent_Click
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70

Exit_cmddeletestudent_Click:
Exit Sub

Err_cmddeletestudent_Click:
MsgBox Err.Description
Resume Exit_cmddeletestudent_Click

End Sub
SEARCHING FOR A STUDENT

Private Sub cmdsearchstudent_Click()
On Error GoTo Err_cmdsearchstudent_Click
Screen.PreviousControl.SetFocus
DoCmd.DoMenuItem acFormBar, acEditMenu, 10, , acMenuVer70

Exit_cmdsearchstudent_Click:
Exit Sub

Err_cmdsearchstudent_Click:
MsgBox Err.Description
Resume Exit_cmdsearchstudent_Click

End Sub

There it is! Absolutely no sense to me but any help would be VERY MUCH
appreciated.

Thanks in advance

Matt
Nov 12 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Matthew,

The first thing to do is to check that the code is actually running. In the
second line of each procedure (you have 3), insert this code:
MsgBox "Hello Matt"

If the message doesn't pop up, your procedure isn't running and you need to
investigate why!

--
PC Datasheet
Your Resource For Help With Access, Excel And Word Applications
re******@pcdatasheet.com
www.pcdatasheet.com
"Mathew Hill" <Co************@hotmail.com> wrote in message
news:1c*************************@posting.google.co m...
I am a beginner to the more technical aspects of Microsoft Access
(2000) and was wondering if any one can help. I have 3 buttons on a
form which add, delete and search for a record. However, when I click
on the respective buttons absolutely nothing happens! I was wondering
if anyone could help? The code I have is below...

ADDING A RECORD

Private Sub cmdaddstudent_Click()
On Error GoTo Err_cmdaddstudent_Click
DoCmd.GoToRecord , , acNewRec

Exit_cmdaddstudent_Click:
Exit Sub

Err_cmdaddstudent_Click:
MsgBox Err.Description
Resume Exit_cmdaddstudent_Click

End Sub
DELETING A RECORD

rivate Sub cmddeletestudent_Click()
On Error GoTo Err_cmddeletestudent_Click
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70

Exit_cmddeletestudent_Click:
Exit Sub

Err_cmddeletestudent_Click:
MsgBox Err.Description
Resume Exit_cmddeletestudent_Click

End Sub
SEARCHING FOR A STUDENT

Private Sub cmdsearchstudent_Click()
On Error GoTo Err_cmdsearchstudent_Click
Screen.PreviousControl.SetFocus
DoCmd.DoMenuItem acFormBar, acEditMenu, 10, , acMenuVer70

Exit_cmdsearchstudent_Click:
Exit Sub

Err_cmdsearchstudent_Click:
MsgBox Err.Description
Resume Exit_cmdsearchstudent_Click

End Sub

There it is! Absolutely no sense to me but any help would be VERY MUCH
appreciated.

Thanks in advance

Matt

Nov 12 '05 #2

P: n/a
Hi,

Yeah I tried that and a message does appear saying 'Hello Matt'. Can you
think of anything else?

Matt

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #3

P: n/a
Co************@hotmail.com (Mathew Hill) wrote in message news:<1c*************************@posting.google.c om>...
I am a beginner to the more technical aspects of Microsoft Access
(2000) and was wondering if any one can help. I have 3 buttons on a
form which add, delete and search for a record. However, when I click
on the respective buttons absolutely nothing happens! I was wondering
if anyone could help? The code I have is below...


I just tried your code (except for adding a missing 'P') in Access 97
and in Access 2K. The form had a Record Source of tblData (bound).
All three buttons worked perfectly. I used the minimum number of
'References' allowed by Access. I also tried deleting all the records
from tblData. The 'New' button wouldn't work in that case. The
other buttons reported "You can't use Find or Replace now."
I tried setting the Form Properties 'Allow Edits', 'Allow Deletions'
and 'Allow Additions' to No. This allowed the Search button to
work but the other buttons reported "You can't go to the specified
record." Something very basic is missing.

I would guess that Access has "lost" the connection between the
command button and the code. Go to design mode and double
click one of the command buttons. If you see [Event Procedure]
to the right of the 'On Click . .' then click on it. You should see
three dots to the far right. Single click on the three dots. If you
see your code come up then something else is the problem.

If you don't see [Event Procedure] or your code then Edit | Undo
Insert Text. The code you want for the button should still be
visible. Go to the code you want and use CTRL X from the On
Error to just before the End Sub. Then delete the Sub declaration
line and the End Sub by highlighting and pressing Delete. Go
back to the form design view and double click on that button.
Click the Event tab then click on the white space to the right
of "On Click." You should see three dots on the far right. Single
click on the three dots. If Choose Builder comes up, choose
"Code Builder." This "reconnects" the code to the button.
The code you want should still be in your edit buffer. Paste
the code in again between the Sub Declaration and the End Sub.

This problem can occur when you paste code behind a form
and then create the command buttons afterwards. Sometimes
Access doesn't automatically find the code.

James A. Fortune
Nov 12 '05 #4

P: n/a
James,

Read his response to my suggestion. He says that he gets "Hello Matt" when he
clicks the buttons so the code has to be connected to the buttons. It's got me
stumped!

Steve
PC Datasheet
"James Fortune" <ja******@oakland.edu> wrote in message
news:a6**************************@posting.google.c om...
Co************@hotmail.com (Mathew Hill) wrote in message

news:<1c*************************@posting.google.c om>...
I am a beginner to the more technical aspects of Microsoft Access
(2000) and was wondering if any one can help. I have 3 buttons on a
form which add, delete and search for a record. However, when I click
on the respective buttons absolutely nothing happens! I was wondering
if anyone could help? The code I have is below...


I just tried your code (except for adding a missing 'P') in Access 97
and in Access 2K. The form had a Record Source of tblData (bound).
All three buttons worked perfectly. I used the minimum number of
'References' allowed by Access. I also tried deleting all the records
from tblData. The 'New' button wouldn't work in that case. The
other buttons reported "You can't use Find or Replace now."
I tried setting the Form Properties 'Allow Edits', 'Allow Deletions'
and 'Allow Additions' to No. This allowed the Search button to
work but the other buttons reported "You can't go to the specified
record." Something very basic is missing.

I would guess that Access has "lost" the connection between the
command button and the code. Go to design mode and double
click one of the command buttons. If you see [Event Procedure]
to the right of the 'On Click . .' then click on it. You should see
three dots to the far right. Single click on the three dots. If you
see your code come up then something else is the problem.

If you don't see [Event Procedure] or your code then Edit | Undo
Insert Text. The code you want for the button should still be
visible. Go to the code you want and use CTRL X from the On
Error to just before the End Sub. Then delete the Sub declaration
line and the End Sub by highlighting and pressing Delete. Go
back to the form design view and double click on that button.
Click the Event tab then click on the white space to the right
of "On Click." You should see three dots on the far right. Single
click on the three dots. If Choose Builder comes up, choose
"Code Builder." This "reconnects" the code to the button.
The code you want should still be in your edit buffer. Paste
the code in again between the Sub Declaration and the End Sub.

This problem can occur when you paste code behind a form
and then create the command buttons afterwards. Sometimes
Access doesn't automatically find the code.

James A. Fortune

Nov 12 '05 #5

P: n/a
"PC Datasheet" <sp**@nospam.spam> wrote in message news:<Cj***************@newsread3.news.atl.earthli nk.net>...
James,

Read his response to my suggestion. He says that he gets "Hello Matt" when he
clicks the buttons so the code has to be connected to the buttons. It's got me
stumped!

Steve
PC Datasheet


Yeah, you're absolutely correct. I thought about it about two minutes after
I hit 'Post.' I tried taking out the Record Source and that just kept
the records on the form from showing up. C'mon Matt, give us another hint!
Do you have other forms with code that run properly? Could the files used
by the references be corrupted? I'm running out of options. Are you just
kidding with us? Is your mouse acting flaky :-)? Throw us Access hounds
a bone. Seriously, we need more information to try to tell what's going on.
What version of Access are you using? Did you do a custom installation that
could have omitted something important? Did you used to have a previous
version of Access on your machine. Something strange is going on.

James A. Fortune
Nov 12 '05 #6

P: n/a
Hi

Thank you VERY much for your help. I deleted all the buttons and then
created them again. Now they seem to be working! Very strange!!

I am having trouble with the search button though. I enter a record on a
form and then test that the search button is working by adding the same
search criteria that I have just inputted on my form. But it says it
cant find it. Which is completely stupid because it is obviously there
as I have just inputted it!

Search Code

Private Sub cmdsearchrecord_Click()
On Error GoTo Err_cmdsearchrecord_Click
Screen.PreviousControl.SetFocus
DoCmd.DoMenuItem acFormBar, acEditMenu, 10, , acMenuVer70

Exit_cmdsearchrecord_Click:
Exit Sub

Err_cmdsearchrecord_Click:
MsgBox Err.Description
Resume Exit_cmdsearchrecord_Click

End Sub
If you could help I would be very grateful.

Thanks

Matt

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #7

P: n/a
Here's a sample database from http://www.utteraccess.com

A Wee Improved Search Engine
http://www.utteraccess.com/forums/sh...b=5&o=&fpart=1
Nov 12 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.