Précédent   Forum des professionnels en informatique > Logiciels > Microsoft Office > Access > VBA Access
VBA Access Le forum pour les questions relatives au code VBA sous Access, et à son environnement de développement VBE.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 21/12/2010, 13h36   #1
Membre du Club
 
Inscription : août 2008
Messages : 86
Détails du profil
Informations forums :
Inscription : août 2008
Messages : 86
Points : 43
Points : 43
Par défaut Problème Import Spreadsheet

Bonjour à tous,

Je cherche à importer des listes dans sous format excel vers une table access. J'utilise la fonction Docmd.Transferspreadsheet ac Import. J'ai un formulaire avec une Private Sub VBA liée, et l'import dans ce cadre se fait bien. Par contre, j'ai besoin de faire de l'import à partir d'un bouton sur un formulaire créé dynamiquement, en faisant appel à un Public Function, et là j'ai l'import ne se fait pas, la table reste vide, sans message d'erreur, je constate que l'import ne se fait tous simplement pas.

Le problème vient à mon avis de la structure même de la fonction, ou le fait que la table ne soit pas liée, je n'en sais rien, avez vous un avis? Merci...

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
 
Public Function import_liste()
 
If MsgBox("Importer cette liste? Si oui sauvegardez et fermez tous vos fichiers excel avant fermeture automatique.", vbYesNo + vbInformation, "Vérification") = vbNo Then
GoTo Fin
End If
 
    Dim strPathToFiles As String
    Dim xlapp As New Excel.Application
    Dim i As Long
    Dim db As Database
Dim Tb As TableDef
Dim a As Boolean
Dim tmpxlwb As Excel.Workbook
Dim tmpxlwsh As Excel.Worksheet
Dim nbligne As Long
Dim strOnglet As String
 
If Forms("Formulaire2").Controls("Chemin_").Caption = "" Or IsNull(Forms("Formulaire2").Controls("Onglet_").Value) Then
MsgBox "Choisissez un fichier et un onglet"
GoTo Fin
End If
 
    'chemin d'acces au fichier excel
 
    strPathToFiles = Forms("Formulaire2").Controls("Chemin_").Caption
    strOnglet = Forms("Formulaire2").Controls("Onglet_").Value
 
'Supprimer les warnings
 DoCmd.SetWarnings False
 
     'Supprimer enregistrement
 
Set db = CurrentDb
 
DoCmd.RunSQL "DELETE * FROM U_Import"
 
On Error Resume Next
Set tmpxlwb = xlapp.Workbooks.Open(strPathToFiles)
 
If (Err.Number <> 0) Then
    MsgBox "Fichier non trouvé."
    GoTo Fin
End If
 
On Error Resume Next
Set tmpxlwsh = tmpxlwb.Sheets(strOnglet)
 
If (Err.Number <> 0) Then
    MsgBox "Onglet non trouvé."
    GoTo Fin
End If
 
nb_ligne = 4
Do While IsEmpty(tmpxlwsh.Cells(nb_ligne, 1).Value) = False
nb_ligne = nb_ligne + 1
    Loop
 
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "U_Import", strPathToFiles, False, Chr(39) & strOnglet & Chr(39) & "!A2:I" & nb_ligne
 
tmpxlwb.Close (False)
xlapp.Quit
 
Set tmpxlwsh = Nothing
Set tmpxlwb = Nothing
Set xlapp = Nothing
 
 DoCmd.SetWarnings True
 
'On kill le processus excel.exe
 
 TermProcess "EXCEL.EXE" 
 
Fin:
 
End Function
Glherbier est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 21/12/2010, 13h51   #2
Membre Expert
 
Inscription : janvier 2006
Messages : 1 111
Détails du profil
Informations forums :
Inscription : janvier 2006
Messages : 1 111
Points : 1 093
Points : 1 093
Bonjour,

tu as essayé de faire du pas à pas en debug ?
(cf. ma signature)
__________________
[Access] Les bases du débogage => ici
Kloun est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 21/12/2010, 14h36   #3
Membre du Club
 
Inscription : août 2008
Messages : 86
Détails du profil
Informations forums :
Inscription : août 2008
Messages : 86
Points : 43
Points : 43
Oui, et tout à l'air de fonctionner normalement... access effectue le docmd sans souci en apparence ,puis passe à le suite... C'est étrange, vraiment étrange

Le pire dans cette affaire, c'est que j'ai beau mettre une commande sciemment fausse comme :

Code :
1
2
3
4
 
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel97, "U_Luberlu", "dekdezdke.dkedoz", False, wowow
 
MsgBox "hello"
,

access continue à fonctionner comme si de rien n'était, sans message d'erreur aucun, et m'affiche même le msgbox qui suit! Comment est-ce possible? Ou est mon erreur?
Glherbier est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 21/12/2010, 15h50   #4
Membre du Club
 
Inscription : août 2008
Messages : 86
Détails du profil
Informations forums :
Inscription : août 2008
Messages : 86
Points : 43
Points : 43
Pour une raison difficilement expliquable, le problème s'est résolu.

Voici le code pour les gens intéressés (car je suis sûr qu'ils sont foison) :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 
Public Function import_liste()
 
If MsgBox("Importer cette liste? Si oui sauvegardez et fermez tous vos fichiers excel avant fermeture automatique.", vbYesNo + vbInformation, "Vérification") = vbNo Then
GoTo Fin
End If
 
    Dim strPathToFiles As String
    Dim xlapp As New Excel.Application
    Dim i As Long
    Dim db As Database
Dim Tb As TableDef
Dim a As Boolean
 
 
Dim tmpxlwb As Excel.Workbook
Dim tmpxlwsh As Excel.Worksheet
Dim nbligne As Long
Dim strOnglet As String
 
If Forms("Formulaire2").Controls("Chemin_").Caption = "" Or IsNull(Forms("Formulaire2").Controls("Onglet_").Value) Then
MsgBox "Choisissez un fichier et un onglet"
GoTo Fin
End If
 
    'chemin d'acces au fichier excel
 
    strPathToFiles = Forms("Formulaire2").Controls("Chemin_").Caption
    strOnglet = Forms("Formulaire2").Controls("Onglet_").Value
 
 
'Supprimer les warnings
 DoCmd.SetWarnings False
 
 
     'Supprimer enregistrement
 
Set db = CurrentDb()
 
DoCmd.RunSQL "DELETE * FROM U_Import"
 
On Error Resume Next
Set tmpxlwb = xlapp.Workbooks.Open(strPathToFiles)
 
If (Err.Number <> 0) Then
    MsgBox "Fichier non trouvé."
    GoTo Fin
End If
 
On Error Resume Next
Set tmpxlwsh = tmpxlwb.Sheets(strOnglet)
 
If (Err.Number <> 0) Then
    MsgBox "Onglet non trouvé."
    GoTo Fin
End If
 
nb_ligne = 4
Do While IsEmpty(tmpxlwsh.Cells(nb_ligne, 1).Value) = False
nb_ligne = nb_ligne + 1
    Loop
 
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "U_Import", strPathToFiles, True, strOnglet & "!A2:I" & nb_ligne 'Chr(39) & strOnglet & Chr(39) & "!A2:I" & nb_ligne
 
tmpxlwb.Close (False)
xlapp.Quit
 
Set tmpxlwsh = Nothing
Set tmpxlwb = Nothing
Set xlapp = Nothing
 
 DoCmd.SetWarnings True
 
 TermProcess "EXCEL.EXE"
 
Fin:
 
End Function
Glherbier est déconnecté   Envoyer un message privé Réponse avec citation 10
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 06h46.


 
 
 
 
Partenaires

Hébergement Web