Solved

Check record exist before using AddNew

Posted on 2006-11-08
7
281 Views
Last Modified: 2012-08-13
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
Comment
Question by:acaraciolo
7 Comments
 
LVL 1

Accepted Solution

by:
thedwalker earned 500 total points
ID: 17901675
try this:
sql="SELECT * FROM WHERE EmpCode = " & EmpCode & " AND  sDate= " & sDate"

If rs.BOF And rs.EOF Then
     rs.AddNew
End If
0
 
LVL 20

Expert Comment

by:jitganguly
ID: 17901860
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
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 17901885
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Expert Comment

by:thedwalker
ID: 17902080
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
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 17902115
@Thedwalker - I see what you mean and I didn't think of that.  Thanks for the correction.

bol
0
 
LVL 1

Expert Comment

by:thedwalker
ID: 17902116
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
 

Author Comment

by:acaraciolo
ID: 17902253
Perfect thedwalker Thanks
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now