Code Help

commint

New member
Local time
Today, 19:08
Joined
Oct 19, 2015
Messages
3
Hey, sorry I didn't know where to post this so thought I'd do it in the general thread.

I'm a beginner to Access and not done coding before, my mate helped me with some code to use in my database for a hotel booking system. The code I got is:

Private Sub Command35_Click()
On Error GoTo Err_Command35_Click
Dim stDocName As String
stDocName = "Room Info"
DoCmd.OpenQuery stDocName, acNormal, acEdit
Exit_Command35_Click:
Exit Sub
Err_Command35_Click:
MsgBox Err.Description
Resume Exit_Command35_Click
End Sub

My question is how do I make an error box appear when someone types in a room number that isn't in the database? (rooms are 100 - 125) I want it to say this room is not available or does not exist.

Also, if anyone can explain what the code above does as I'm clueless with this thing but have got an interest for it.
 
the above code just opens your "Room Info" form.

you must put a code in your Room No control in Room Info form to validate user input.
 
ORIGINAL CODE

Code:
Private Sub Command35_Click()
On Error GoTo Err_Command35_Click
Dim stDocName As String
stDocName = "Room Info"
DoCmd.OpenQuery stDocName, acNormal, acEdit
Exit_Command35_Click:
Exit Sub
Err_Command35_Click:
MsgBox Err.Description
Resume Exit_Command35_Click
End Sub

MY EDITS AND NOTES:

1. NAME YOUR BUTTON! and recreate its event handler sub.
2. Shorten the code.


This version is for your learning purposes only and describes the process.
Code:
Private Sub Command35_Click()
On Error GoTo err_handler

	' Declare stDocName variable
	Dim stDocName As String

	' Initialize stDocName's value to "Room Info"
	stDocName = "Room Info"


	' Open Query by the name of "Room Info", in a normal way (view), and allow for edits.
	DoCmd.OpenQuery stDocName, acNormal, acEdit

	Exit Sub

err_handler: MsgBox Err.Description
End Sub

This is the shorter version.
Code:
Private Sub btnOpenRoomInfoQuery_Click()
On Error GoTo err_handler

	' Open Query by the name of "Room Info", in a normal way (view), and allow for edits.
	' Instead of declaring and initializing a variable. Just pass the string directly to the function! :)
	DoCmd.OpenQuery "Room Info", acNormal, acEdit

	Exit Sub

err_handler: MsgBox Err.Description
End Sub
 
I have this code which runs my query and opens the results in a separate form..

Private Sub Command13_Click()
On Error GoTo Err_Command13_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Rooms Available and Info"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_Command13_Click:
Exit Sub

Err_Command13_Click:
MsgBox Err.Description
Resume Exit_Command13_Click

End Sub

Any ideas why this code won't work on 2010? (I'm using 2013 but need it to run in 2010)
 
Last edited:

Users who are viewing this thread

Back
Top Bottom