Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

Check record exist before using AddNew

Hello all:  I have a form that inputs data to a database to be used to create a work schedule.  I am entering:
EmpCode                   'the employee code
sTime                        'start time
eTime                        'end time
sDate                         'start date
eDate                         'end date
lead                           'yes if lead no if not
EmpName                   'full name
city                             'location city of technician

I do the following to add this data to the database:

            strDataPath = Server.MapPath("/WklySchedule.mdb")
            strConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;"_
          & " Data Source= " & strDataPath & ";"_
          & " Mode=Share Deny None;User Id=admin;PASSWORD=;"


            set Conn=Server.CreateObject("ADODB.Connection")
            Conn.ConnectionTimeout = 15
            Conn.CommandTimeout =  10
            Conn.Mode = 3 'adModeReadWrite
            
            if Conn.state = 0 then
                Conn.Open strConnectString
            end if
              
              set rs=Server.CreateObject("ADODB.recordset")
              sql = "SELECT * FROM tblSchedule"
              rs.Open sql, Conn,3,3

                  rs.AddNew
                  rs.Fields("EmpCode") = EmpCode
                  rs.Fields("sTime") = sTime
                  rs.Fields("eTime") = eTime
                  rs.Fields("sDate") = sDate
                  rs.Fields("eDate") = eDate
                  rs.Fields("lead") = lead
                  rs.Fields("EmpName") = EmpName
                  rs.Fields("city") = city
                  rs.Fields("ipa") = ipa
                  rs.Fields("date") = dos
                  rs.fields("time") = tos
                  rs.Update

                  rs.close
                  Conn.close
            
                  response.clear
                  response.redirect(BackToForm)

This works but I want the data to overwrite existing data where the EmpCode and sDate already exist.  For instance if a manager schedules an employee to work 0000 - 0800 on 11/11/2006 but later decides to change the times to 0930 - 1800 on 11/11/2006... I want the program to simply overwrite the orignal data.  Is there a simple way to see if the EmpCode and sDate already exist and update rather then add?
0
acaraciolo
Asked:
acaraciolo
1 Solution
 
thedwalkerCommented:
try this:
sql="SELECT * FROM WHERE EmpCode = " & EmpCode & " AND  sDate= " & sDate"

If rs.BOF And rs.EOF Then
     rs.AddNew
End If
0
 
jitgangulyCommented:
This should work for you

sql = "SELECT * FROM tblSchedule where EmpCode= " & EmpCode & " and stime = #" & sTime &"#  and eTime=#" & etime & "#"
rs.Open sql, Conn,3,3

If rs.eof ' new recored
rs.addNew
end if

0
 
b0lsc0ttCommented:
acaraciolo,

Thedwalker's response shows a key and may be all you need.  However to actually do what you wanted you will want to include an Else in that If to update the record.  The test will be true if no record is found.  If it is found then you can use Update to update the record with the new values.  Make sure that you have opened the database to allow updating.

If rs.EOF then
   ' steps to add the record
Else
  ' update the fields with something like this rs.fieldname = variableWithNewValue
  rs.update
End if

Let me know if you have any questions or need more information.

b0lsc0tt
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
thedwalkerCommented:
Maybe I should have written the whole thing out the snippet of code that I wrote was to replace the plain AddNew line. It doesn't need an Else clause because when there is a record then the existing code will just update that. At least that is how I have done it before and it worked fine.
0
 
b0lsc0ttCommented:
@Thedwalker - I see what you mean and I didn't think of that.  Thanks for the correction.

bol
0
 
thedwalkerCommented:
So your code would look like this:

 strDataPath = Server.MapPath("/WklySchedule.mdb")
          strConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;"_
          & " Data Source= " & strDataPath & ";"_
          & " Mode=Share Deny None;User Id=admin;PASSWORD=;"


          set Conn=Server.CreateObject("ADODB.Connection")
          Conn.ConnectionTimeout = 15
          Conn.CommandTimeout =  10
          Conn.Mode = 3 'adModeReadWrite
         
          if Conn.state = 0 then
              Conn.Open strConnectString
          end if
           
            set rs=Server.CreateObject("ADODB.recordset")
            sql = "SELECT * FROM WHERE EmpCode = '" & EmpCode & "' AND  sDate= '" & sDate"'"
            rs.Open sql, Conn,3,3

               if rs.BOF and rs.EOF then
                    rs.AddNew
               end if

               rs.Fields("EmpCode") = EmpCode
               rs.Fields("sTime") = sTime
               rs.Fields("eTime") = eTime
               rs.Fields("sDate") = sDate
               rs.Fields("eDate") = eDate
               rs.Fields("lead") = lead
               rs.Fields("EmpName") = EmpName
               rs.Fields("city") = city
               rs.Fields("ipa") = ipa
               rs.Fields("date") = dos
               rs.fields("time") = tos
               rs.Update

               rs.close
               Conn.close
         
               response.clear
               response.redirect(BackToForm)
0
 
acaracioloAuthor Commented:
Perfect thedwalker Thanks
0

Featured Post

Technology Partners: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now