Avatar billede dougheffernan Nybegynder
06. juli 2006 - 11:53 Der er 9 kommentarer og
1 løsning

Opdatere data hvis eksisterer i forvejen

Følgende kode bruges til at indsætte data, men hvis dataene eksisterer i forvejen, så går koden i fejl, da PK består af nogle af felterne. Men er det muligt i .Update statementet at checke returkoden og så gå videre til de næste data?

            With rsRecordSet
                .AddNew
                .Fields("Date") = datDato
                .Fields("Shift") = intShift
                For intX = 1 To UBound(varDataHeaders, 2)
                    .Fields(varDataHeaders(1, intX)) = rngDataArea(intR, intX)
                Next
                .Update
            End With
Avatar billede dougheffernan Nybegynder
06. juli 2006 - 12:12 #1
Alternativet er at slette de gamle records først og så tilføje de nye.
Avatar billede terry Ekspert
06. juli 2006 - 12:44 #2
You could use an SQL to check if the records exists and if so go on to then next record. You can also use the dcount() function to see if it exists.
And a quick and dirt fix would be to catch the error and then go to the next record
Avatar billede dougheffernan Nybegynder
07. juli 2006 - 08:50 #3
Exactly!
The only problem is how I make the code jump to the next record (in the above code)
Avatar billede dougheffernan Nybegynder
07. juli 2006 - 08:50 #4
And I can't use DCount.....it's data from an Excel spreadsheet that gets pushed into the database...
Avatar billede dougheffernan Nybegynder
07. juli 2006 - 08:56 #5
I like the "catch error and move to next record"... ;)
Avatar billede terry Ekspert
07. juli 2006 - 09:31 #6
Did you find out how to move to the next record?

If the Excel sheet gets imported into Access first then you could use DCount. But it was maybe an ide ato try selecting and if it exist replace .AddNew with .Edit
Avatar billede dougheffernan Nybegynder
07. juli 2006 - 10:39 #7
I just found out that it's quite alright to delete the data first, then transfer them again, but how (from Excel)?

I need to delete all data that has a particular Date, Shift and ID. How?

Dim cnConnection As ADODB.Connection
Dim rsRecordSetDelete As ADODB.Recordset

cnConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Database.mdb"
cnConnection.Open

Set rsRecordSet = New ADODB.Recordset

rsRecordSet.Open strTable, cnConnection, adOpenKeyset, adLockOptimistic, adCmdTable

With rsRecordSetDelete
  .Delete
  .?
  .?
End With
Avatar billede terry Ekspert
07. juli 2006 - 10:48 #8
Dont use ADO muchso this i sjust a guess

cnConnection.Execute ("DELETE * FROM " & strTable ,, adCmdText)
Avatar billede dougheffernan Nybegynder
10. juli 2006 - 08:48 #9
You are correct, terry, thanks for your help.
Avatar billede terry Ekspert
10. juli 2006 - 08:57 #10
selv tak, og god dag
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester