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
Solved

MS Access VBA Questions

Posted on 2013-01-08
8
242 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
  • 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

837 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