'##### insert the news ones ##### 'If there is already values in the DB If testPresenceHotfix(dbs, stock) = True Then 'initialization of the recordset before updating sql1 "select * from HOTFIX where ID_SERVER" & stock Set rs = dbs.OpenRecordset(sql1, dbOpenDynaset) 'for each hotfix value in the excel field, we verify its presence in the db For j = 1 To retour a = 0 While Not rs.EOF str2 = Séparer(iHotfixs, ";").PosAlr(j) str1 = rs.Fields("HOTFIX") If StrComp(str1, str2, vbTextCompare) = 0 Then a = a + 1 End If rs.MoveNext Wend 'if the hotfix value for the server is already in the database, a will be > 0 so if a=0 weZve got to introduct the value in the DB If (a = 0) Then MsgBox ("hot manquant => insertion" & a) sql1 = "INSERT INTO HOTFIX (ID_SERVER,HOTFIX) values (" & stock & ",'" & Séparer(iHotfixs, ";").PosAlr(j) & "');" dbs.Execute sql1 End If Next rs.Close End If
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPrivate Sub Cmd_Importation_Click() ' all the var. we need ( string, integer, recordset, database ) Dim PathFic As String Dim NomFic As String Dim NomFicXLS As String Dim iDomain As String Dim iServer_Name As String Dim iHotfixs As String Dim iOS_Version As String Dim i As Integer Dim j As Integer Dim k As Integer Dim sql1 As String Dim dbs As database Dim retour As String Dim stock As Integer Dim rs As Recordset Dim a As Integer Dim str1 As String Dim str2 As String 'initialization of the objects we use Set dbs = CurrentDb Set ClasseurXLS = CreateObject("Excel.application") 'Initialization : ask the name of the file If (Text1.value <> "") Then NomFic = Text1 NomFic = "" & NomFic Else MsgBox ("The fileZs name is missing") Exit Sub End If 'Initialization : ask the way to obtain the file If (Text2.value <> "") Then PathFic = Text2 Else MsgBox ("The folderZs name is missing") Exit Sub End If 'Open the Worbook to import the informations ClasseurXLS.Workbooks.Open PathFic & NomFic ' ##################### For every server of the excel file ################################# '!!!!!!!!!!!!!! To use the recordsets the Microsoft DAO 2.5/3.5 Compatibility Library is needed !!!!!!!!!!!!! 'We donZt start at the first line because thatZs the columnsZdescriptions => i = 2 i = 2 Do While ClasseurXLS.cells(i, 1) <> "" 'Save the excelZinformations into different var ( string, integer ) iDomain = ClasseurXLS.cells(i, 1) iServer_Name = ClasseurXLS.cells(i, 3) iHotfixs = ClasseurXLS.cells(i, 7) 'HOTFIXS : how many hotfixs values are in the Hotfixs field of the excel file retour = Séparer(iHotfixs, ";").Count '###################### Basic Data and Server recognition ######################## '##### Is the server in the access DB ? ##### 'If yes Update If testPresenceServer(dbs, iServer_Name) = True Then 'initialization of the recordset before updating sql1 "select * from MAINTABLE where NAME_SERVER'" & iServer_Name & "'" Set rs = dbs.OpenRecordset(sql1, dbOpenDynaset) 'save the ID_SERVER for the HotfixsZupdate stock = rs.Fields("ID_SERVER") rs.Edit 'Update rs.Fields("DOMAIN") = iDomain 'End of the Update rs.Update rs.Close End If 'If no, we have to create a new line in the access DB If testPresenceServer(dbs, iServer_Name) = False Then 'insert the new server thanks to a sql request sql1 = "INSERT INTO MAINTABLE (NAME_SERVER,DOMAIN) values ('" & iServer_Name & "','" & iDomain & "');" dbs.Execute sql1 'save the ID_SERVER for the HotfixsZupdate sql1 "select * from MAINTABLE where NAME_SERVER'" & iServer_Name & "'" Set rs = dbs.OpenRecordset(sql1, dbOpenDynaset) stock = rs.Fields("ID_SERVER") rs.Close End If '############################################################################################ '###################### HOTFIXSZUPDATE ######################## '##### delete hotfixs which are not available ##### 'initialization of the recordset sql1 "select * from HOTFIX where ID_SERVER" & stock Set rs = dbs.OpenRecordset(sql1, dbOpenDynaset) 'for every record of the recordset we test While Not rs.EOF a = 0 str1 = rs.Fields("HOTFIX") For j = 1 To retour str2 = Séparer(iHotfixs, ";").PosAlr(j) If StrComp(str1, str2, vbTextCompare) = 0 Then a = a + 1 End If Next 'if the hotfix values for this server in the DB doesnZt exist in the field of the excel file, we donZt increment a => a = 0 If a = 0 Then MsgBox ("efface") sql1 " delete * from hotfixs where ID_SERVER " & stock & " and HOTFIX '" & rs.Fields("HOTFIX") & "'" End If rs.MoveNext Wend '##### insert the news ones ##### 'If there is already values in the DB If testPresenceHotfix(dbs, stock) = True Then 'initialization of the recordset before updating sql1 "select * from HOTFIX where ID_SERVER" & stock Set rs = dbs.OpenRecordset(sql1, dbOpenDynaset) 'for each hotfix value in the excel field, we verify its presence in the db For j = 1 To retour a = 1 While Not rs.EOF If StrComp(str1, str2) = 0 Then a = 0 End If rs.MoveNext Wend 'if the hotfix value for the server is already in the database, a will be > 0 so if a=0 weZve got to introduct the value in the DB If a = 0 Then MsgBox ("hot manquant => insertion" & a) sql1 = "INSERT INTO HOTFIX (ID_SERVER,HOTFIX) values (" & stock & ",'" & Séparer(iHotfixs, ";").PosAlr(j) & "');" dbs.Execute sql1 End If Next rs.Close End If 'if the DB is empty If testPresenceHotfix(dbs, stock) = False Then For j = 1 To retour MsgBox ("table vide insertion primaire") sql1 = "INSERT INTO HOTFIX (ID_SERVER,HOTFIX) values (" & stock & ",'" & Séparer(iHotfixs, ";").PosAlr(j) & "');" dbs.Execute sql1 Next End If '############################################################################################### 'incrementation of i => next line in the excel file i = i + 1 Loop ' ############################################## End of the Update ################################################ 'closing the Workbook ClasseurXLS.Workbooks.Close MsgBox ("Update OK") End Sub
' we define a special type for the function Séparer Public Type SEPRR PosRev(100) As String PosAlr(100) As String Count As String End Type ' With this function we will have three to use use it : ' 1 - Séparer( ........, symbol).Count : return the number of occurence in the input string ' 2 - Séparer( ........, symbol).PosRev(X) : return the X th string from the right side ' 3 - Séparer( ........, symbol).PosAlr(X) : return the X th string from the left side Public Function Séparer(Chaine As String, Séparateur As String) As SEPRR Dim IndX, IndX2, Lpos Séparer.PosRev(0) = Chaine Lpos = 1 Séparer.Count = 0 IndX = 0 For IndX2 = Lpos To Len(Chaine) - Lpos If Lpos < Len(Chaine) Then If Mid(Chaine, IndX2, Len(Séparateur)) = Séparateur Then IndX = IndX + 1 Séparer.PosAlr(IndX) = Mid(Chaine, Lpos, IndX2 - Lpos) Lpos = IndX2 + Len(Séparateur) Séparer.Count = Séparer.Count + 1 End If Else End If Next Séparer.PosAlr(IndX + 1) = Right(Chaine, Len(Chaine) - Lpos + 1) Séparer.Count = Séparer.Count + 1 For IndX = 1 To Séparer.Count Séparer.PosRev(Séparer.Count - IndX + 1) = Séparer.PosAlr(IndX) Next End Function