Current location - Training Enrollment Network - Mathematics courses - How to import data from access into excel and realize it with VBA programming?
How to import data from access into excel and realize it with VBA programming?
Public sub-import data ()

Take my data as a string, my table as a string and SQL as a string.

Dim cnn is ADODB. relationship

Dim rs is ADODB. Record set

ActiveSheet。 Cell, secure

mydata = ThisWorkbook。 Path and path. "\ Score management. Mdb "'specifies the database.

Mytable = "test scores "'specifies the data table.

Establish a connection with the database.

Set cnn = New ADODB. relationship

With cnn

. provider = " Microsoft . jet . oledb . 4.0 "

. Open my data

End with ...

SQL = "select class, avg (mathematics) as the mathematical average, avg (Chinese) as the language average, "_

& "Physical average is physical average, chemical average is chemical average, and English average is English average." _

& amp" avg (sports) as the average of sports, avg (total score) as the average of total scores "_

& "From" & My desk & "Grouped by category"

Set rs = New ADODB。 Record set

rs。 Open SQL, cnn, adOpenKeyset, adlockoptimal.

Copy field name

For i = 1 to rs. Field. count

Cells( 1,i) = rs。 Field (i-1). name

Next, I

Copy all data

Range ("A2"). Copy from recordset rs

rs。 close

cnn。 close

Set rs = none

Set cnn = None.

End joint