Premesso che di programmazione capisco poco, sto costruendo un programma per archiviare statistiche di una squadra di pallacanestro. Il database si chiama statistiche.mdb e all'interno c'è una tabella statistiche formata dalla colonna "giocatore", e da colonne A,1,2,3 ecc...
Allora, con Questo codice aggiungo i nuovi Record,e fin qui va tutto bene.
La domanda è: Mi connetto al database, ma poi lo chiudo?? Che sintassi devo aggiungere eventualmente?
Questo è il codice:
stringa = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
stringa = stringa & "C:\test\statistiche.mdb"
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.Open stringa
rs.Open "statistiche", cn, 3, 3
rs.AddNew
rs("giocatore") = giocatore
rs("A") = 0
rs("2") = 1
rs("3") = 2
rs("4") = 3
rs("5") = 4
rs("6") = 5
rs("7") = 6
rs("8") = 7
rs("9") = 8
rs("10") = 9
rs("11") = 10
rs("12") = 11
rs("13") = 12
rs("14") = 13
rs("15") = 14
rs("16") = 15
rs("17") = 16
rs.Update
Set cn = Nothing
Set rs = Nothing
Il database si forma come desidero, con il codice successivo riesco a estrarre i dati che mi servono per altri usi:
Private Sub Command1_Click()
stringa = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
stringa = stringa & "C:\test\statistiche.mdb"
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.Open stringa
rs.Open "SELECT * FROM statistiche WHERE giocatore= 'Cognome Nome' ", cn, 1
If rs.EOF Then
lblDati.Caption = "Nessun dato"
Else
lblDati.Caption = lblDati.Caption _
& rs("GIOCATORE") & " " _
& rs("A") & vbCrLf
End If
Set cn = Nothing
Set rs = Nothing
La domanda è la stessa, il database si chiude???
Ma ora arriva il problema principale, non riesco ad aggiornare la tabella e neanche ad eliminare un record, tale sintassi l'ho letta su alcuni manuali che ho consultato, ma evidentemente manca qualcosa:
stringa = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
stringa = stringa & "C:\test\statistiche.mdb"
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.Open stringa
rs.Open "UPDATE statistiche SET A='10' WHERE giocatore= 'Cognome e Nome' ", cn, 1
Set cn = Nothing
Set rs = Nothing
In questo caso, Runtime Error 'Per l'operazione è necessaria una query aggiornabile'.
Cosa devo fare affinchè il tutto funzioni?
Io voglio aggiornare la tabella statistiche,e in corrispondenza del record 'Cognome E Nome' Aggiornare a '10' la colonna A
Lo stesso discorso, cioè non funziona,si ha quando voglio eliminare un record
stringa = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
stringa = stringa & "C:\test\statistiche.mdb"
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.Open stringa
rs.Open "DELETE FROM statistiche WHERE giocatore= 'Cognome e Nome' ", cn, 1
Set cn = Nothing
Set rs = Nothing
Ringrazio sin d'ora coloro che vorranno aiutarmi. Purtroppo la mia preparazione è scarsa e da autodidatta e dagli interventi sul forum ho imparato molto. Grazie