Solved

MS Access VBA Questions

Posted on 2013-01-08
8
257 Views
Last Modified: 2013-01-08
The code below inserts start and end dates for a employee with a reason to produce a report that lists events.

How do I add the following features to the code below?

If cboemp (employee is not selected) display message " Please select employee"
If cboreason (reason is not selected) display message " Please select reason"
If txtEnd (end date is not selected) display message " Please select end date"
If txtStart (start date is not selected) display message " Please select start date"

I.E. Don't perform any action until all fields are filled in.

I would also like to add a delete button to delete records according to start and end date.


Private Sub btnUpdate_Click()
Dim strSQL As String
Dim dtmNextDate As Date
dtmNextDate = Me.txtStart
    Do Until dtmNextDate = Me.txtEnd + 1
        strSQL = "INSERT INTO tblEmpTimeOff (etoDate,eto_EmpID,etoReasonCode) VALUES(" & "#" & dtmNextDate & "#" & ", " & Me.cboEmp & ", " & Me.cboReason & ")"
        CurrentDb.Execute strSQL, dbFailOnError
        dtmNextDate = dtmNextDate + 1
    Loop
    Me.cboEmp = ""
    Me.cboReason = ""
    Me.txtEnd = ""
    Me.txtStart = ""
    MsgBox "Input Recieved, Thank You"
End Sub

Open in new window

0
Comment
Question by:DJPr0
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 400 total points
ID: 38754745
try this

Private Sub btnUpdate_Click()
Dim strSQL As String
Dim dtmNextDate As Date

if Me.cboEmp.listindex=-1 then
	msgbox " Please select employee"
	me.cboEmp.setfocus
	exit sub
end if

if Me.cboReason.listindex=-1 then
	msgbox " Please select reason"
	me.cboReason.setfocus
	exit sub
end if

if me.txtEnd & ""="" then
   msgbox " Please select end date"
   me.txtend.setfocus
   exit sub

end if

if me.txtstart & ""="" then
   msgbox " Please select start date"
   me.txtstart.setfocus
   exit sub

end if

dtmNextDate = Me.txtStart
    Do Until dtmNextDate = Me.txtEnd + 1
        strSQL = "INSERT INTO tblEmpTimeOff (etoDate,eto_EmpID,etoReasonCode) VALUES(" & "#" & dtmNextDate & "#" & ", " & Me.cboEmp & ", " & Me.cboReason & ")"
        CurrentDb.Execute strSQL, dbFailOnError
        dtmNextDate = dtmNextDate + 1
    Loop
    Me.cboEmp = ""
    Me.cboReason = ""
    Me.txtEnd = ""
    Me.txtStart = ""
    MsgBox "Input Recieved, Thank You"
End Sub

Open in new window

0
 
LVL 61

Expert Comment

by:mbizup
ID: 38754756
Try this...

Private Sub btnUpdate_Click()
Dim strSQL As String
Dim dtmNextDate As Date
dtmNextDate = Me.txtStart

If cboemp & "" = "" then

     msgbox  " Please select employee"
     me.cboemployee.setfocus
      exit sub
end if

If cboreason & "" = "" then

     msgbox  " Please select reason"
     me.cboreason.setfocus
     exit sub

end if

If txtEnd & "" = "" then

     msgbox  " Please select end date"
     me.txtEnd.setfocus
      exit sub

end if

If txtStart & "" = "" then

     msgbox  " Please select start date"
     me.txtStart.setfocus
      exit sub

end if



    Do Until dtmNextDate = Me.txtEnd + 1
        strSQL = "INSERT INTO tblEmpTimeOff (etoDate,eto_EmpID,etoReasonCode) VALUES(" & "#" & dtmNextDate & "#" & ", " & Me.cboEmp & ", " & Me.cboReason & ")"
        CurrentDb.Execute strSQL, dbFailOnError
        dtmNextDate = dtmNextDate + 1
    Loop
    Me.cboEmp = ""
    Me.cboReason = ""
    Me.txtEnd = ""
    Me.txtStart = ""
    MsgBox "Input Recieved, Thank You"
End Sub 

Open in new window

0
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 50 total points
ID: 38754794
<<I would also like to add a delete button to delete records according to start and end date.>>

The Delete button code would be the same overall.  You'd just need to use a different SQL String:


       
strSQL = "DELETE * FROM  tblEmpTimeOff WHERE etoDate BETWEEN #"  & me.txtStart & "# AND #" & me.txtEnd & "#"  

Open in new window

0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 77

Assisted Solution

by:peter57r
peter57r earned 50 total points
ID: 38754815
Giving users such a powerful delete button is asking for trouble.  I'd think twice about about how you deal with that situation.  

In general, I don't allow users to delete anything and simply mark records as 'deleted' and exclude them from all data sources.
0
 

Author Comment

by:DJPr0
ID: 38754898
Is mark records as deleted different from this statement:
strSQL = "DELETE * FROM  tblEmpTimeOff WHERE etoDate BETWEEN #"  & me.txtStart & "# AND #" & me.txtEnd & "#"  

Open in new window


Does mark records as deleted - have a restoring option?
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 400 total points
ID: 38754922
<Does mark records as deleted - have a restoring option? >

Yes,

But to mark records, you will use an update query, something like this

strSql="Update tblEmpTimeOff set [nameoffield]='InActive' WHERE etoDate BETWEEN #"  & me.txtStart & "# AND #" & me.txtEnd & "#"
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 400 total points
ID: 38754933
or use a Yes/No field named [InActive]

strSql="Update tblEmpTimeOff set [InActive]= -1 WHERE etoDate BETWEEN #"  & me.txtStart & "# AND #" & me.txtEnd & "#"

to restore the records, just reverse the process

strSql="Update tblEmpTimeOff set [InActive]= 0 WHERE etoDate BETWEEN #"  & me.txtStart & "# AND #" & me.txtEnd & "#"
0
 

Author Closing Comment

by:DJPr0
ID: 38754995
Thanks capricorn1!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question