SlideShare a Scribd company logo
Εισηγητής: Μπαλατσούκας Νίκος
Sub test_()
'Declaring the necessary variables.
Dim con As Object
Dim rs As Object
Dim sql As String
Dim myValues() As String
Dim i As Integer
Dim j As Integer
'Disable screen flickering.
Application.ScreenUpdating = False
On Error Resume Next
'Create the ADODB connection object.
Set con = CreateObject("ADODB.connection")
'Check if the object was created.
If Err.Number <> 0Then
MsgBox "Connection was not created!", vbCritical, "Connection error"
Exit Sub
End If
On Error GoTo 0
'Open the connection.
'Όλα σε μια γραμμή:
con.Open
"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=
C:UsersnikosDesktopdbnikos.accdb;"
'Create the SQL statement to read the file.
Filter all the data from Canada.
'Note that the filename is used instead of the
table name.
sql = "SELECT * FROM nikos"
On Error Resume Next
'Create the ADODB recordset object.
Set rs = CreateObject("ADODB.recordset")
'Check if the object was created.
If Err.Number <> 0Then
MsgBox "Connection was not created!", vbCritical, "Connection error"
Exit Sub
End If
On Error GoTo 0
'Set thee cursor location.
rs.CursorLocation = 3 'adUseClient on early binding
rs.CursorType = 1 'adOpenKeyset on early binding
'Open the recordset.
rs.Open sql, con
'Redim the table that will contain the filtered data.
ReDim myValues(rs.RecordCount, 4)
'Loop through the recordset and pass the
selected values to the array.
i = 1
If Not (rs.EOFAnd rs.BOF)Then
'Go to the first record.
rs.MoveFirst
Do Until rs.EOF =True
myValues(i, 1) = rs!onoma
myValues(i, 2) = rs!onoma
myValues(i, 3) = rs!onoma
myValues(i, 4) = rs!onoma
'Move to the next record.
rs.MoveNext
i = i + 1
Loop
Else
Else
'Close the recordet and the connection.
rs.Close
con.Close
'Release the objects.
Set rs = Nothing
Set con = Nothing
'Enable the screen.
Application.ScreenUpdating =True
'In case of an empty recordset display an error.
MsgBox "There are no records in the recordset!", vbCritical, "No Records"
Exit Sub
End If
'Write the array in the sheet.
'Sheet1.Activate
For i = 1To UBound(myValues)
For j = 1To 4
Cells(i + 1, j) = myValues(i, j)
Next j
Next i
'Close the recordet and the connection.
rs.Close
con.Close
'Release the objects.
Set rs = Nothing
Set con = Nothing
'Adust the columns width.
Columns("A:D").EntireColumn.AutoFit
'Enable the screen.
Application.ScreenUpdating =True
'Inform the user that the macro was executed
successfully.
MsgBox "The values were read from recordset
successfully!", vbInformation, "Done"
End Sub

More Related Content

DOCX
Ete programs
PPTX
jQuery basics for Beginners
PDF
Json.parse() in JavaScript
PPTX
Promises, promises, and then observables
PDF
Excel to Use Rest API to communicate with a server
DOC
Quanlycanbo
KEY
ActiveRecord is Rotting Your Brian
PPTX
Vs c# lecture11
Ete programs
jQuery basics for Beginners
Json.parse() in JavaScript
Promises, promises, and then observables
Excel to Use Rest API to communicate with a server
Quanlycanbo
ActiveRecord is Rotting Your Brian
Vs c# lecture11

What's hot (8)

PPTX
JS Objects manipulation
PDF
Java Week2(C) Notepad
PPTX
"Query Execution: Expectation - Reality (Level 300)" Денис Резник
TXT
Vb database connections
DOCX
Codes
PPTX
Building a blockchain part 3
TXT
Examplecode
DOCX
Exp 6.3 d-422 (2)
JS Objects manipulation
Java Week2(C) Notepad
"Query Execution: Expectation - Reality (Level 300)" Денис Резник
Vb database connections
Codes
Building a blockchain part 3
Examplecode
Exp 6.3 d-422 (2)
Ad

Similar to Χρήση Vba για την πρόσβαση σε βάση δεδομένων (20)

TXT
Database connectivity in VBA
PPTX
Is2215 lecture8 relational_databases
PPT
AVB202 Intermediate Microsoft Access VBA
PDF
Useful macros and functions for excel
PDF
DOCX
Inventory program in mca p1
PPTX
A Matter Of Form: Access Forms to make reporting a snap (or a click)
PDF
Inventory management
PPTX
MS Access teaching powerpoint tasks
PDF
Assist9 bmis
PPTX
Data base connectivity and flex grid in vb
PDF
Access tips access and sql part 6 dynamic reports
PPT
Migrate Microsoft Access to SQL Server
DOCX
โครงการ 5 บท
DOCX
โครงการ 5 บท
PPTX
Business Intelligence Portfolio
DOCX
How to work a database
PDF
TXT
Coding pilkades
PDF
Bt0082 visual basic2
Database connectivity in VBA
Is2215 lecture8 relational_databases
AVB202 Intermediate Microsoft Access VBA
Useful macros and functions for excel
Inventory program in mca p1
A Matter Of Form: Access Forms to make reporting a snap (or a click)
Inventory management
MS Access teaching powerpoint tasks
Assist9 bmis
Data base connectivity and flex grid in vb
Access tips access and sql part 6 dynamic reports
Migrate Microsoft Access to SQL Server
โครงการ 5 บท
โครงการ 5 บท
Business Intelligence Portfolio
How to work a database
Coding pilkades
Bt0082 visual basic2
Ad

More from Nikos Mpalatsoukas (20)

PPTX
Μαζική αποστολή email
PPT
Vba παρουσίαση
PPTX
Δημιουργία βιογραφικού europass cv
PPTX
Διαφοροποιημένη διδασκαλία στην πληροφορική
PPTX
Κανονικοποίηση βάσης δεδομένων
PPTX
Παραδείγματα SQL
PPTX
Εισαγωγή στην SQL
PPTX
Wordpress administrator home-page
PPTX
Photoshop levels πολύ φωτεινή εικόνα
PPTX
Photoshop curves και levels σε πολύ σκοτεινή εικόνα
PPTX
Εγκατάσταση tux-paint
PPTX
Οδηγίες για την εξ αποστάσεως εκπαίδευση με το webex
PPTX
Ασφάλεια στο webex
PPTX
Σύνδεση εκπαιδευόμενου στο Webex μέσω smartphone
PPTX
Σύνδεση εκπαιδευόμενου στο Webex μέσω υπολογιστή
PPTX
Πως να κάνετε activate το Webex
PPT
GDPR GAP Analysis
PPT
Εκτίμηση αντίκτυπου σχετικά με την προστασία δεδομένων
PPT
Χαρτογράφηση ροής δεδομένων Data Flow Mapping
PPT
Η λίστα ελέγχου συμβατότητας με το Γενικό Κανονισμο προστασίας Δεδομένων
Μαζική αποστολή email
Vba παρουσίαση
Δημιουργία βιογραφικού europass cv
Διαφοροποιημένη διδασκαλία στην πληροφορική
Κανονικοποίηση βάσης δεδομένων
Παραδείγματα SQL
Εισαγωγή στην SQL
Wordpress administrator home-page
Photoshop levels πολύ φωτεινή εικόνα
Photoshop curves και levels σε πολύ σκοτεινή εικόνα
Εγκατάσταση tux-paint
Οδηγίες για την εξ αποστάσεως εκπαίδευση με το webex
Ασφάλεια στο webex
Σύνδεση εκπαιδευόμενου στο Webex μέσω smartphone
Σύνδεση εκπαιδευόμενου στο Webex μέσω υπολογιστή
Πως να κάνετε activate το Webex
GDPR GAP Analysis
Εκτίμηση αντίκτυπου σχετικά με την προστασία δεδομένων
Χαρτογράφηση ροής δεδομένων Data Flow Mapping
Η λίστα ελέγχου συμβατότητας με το Γενικό Κανονισμο προστασίας Δεδομένων

Recently uploaded (20)

PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
What if we spent less time fighting change, and more time building what’s rig...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Trump Administration's workforce development strategy
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
LDMMIA Reiki Yoga Finals Review Spring Summer
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
What if we spent less time fighting change, and more time building what’s rig...
Microbial diseases, their pathogenesis and prophylaxis
Trump Administration's workforce development strategy
Orientation - ARALprogram of Deped to the Parents.pptx
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Yogi Goddess Pres Conference Studio Updates
2.FourierTransform-ShortQuestionswithAnswers.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Final Presentation General Medicine 03-08-2024.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Paper A Mock Exam 9_ Attempt review.pdf.
Final Presentation General Medicine 03-08-2024.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf

Χρήση Vba για την πρόσβαση σε βάση δεδομένων

  • 2. Sub test_() 'Declaring the necessary variables. Dim con As Object Dim rs As Object Dim sql As String Dim myValues() As String Dim i As Integer Dim j As Integer
  • 4. On Error Resume Next 'Create the ADODB connection object. Set con = CreateObject("ADODB.connection") 'Check if the object was created. If Err.Number <> 0Then MsgBox "Connection was not created!", vbCritical, "Connection error" Exit Sub End If On Error GoTo 0
  • 5. 'Open the connection. 'Όλα σε μια γραμμή: con.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source= C:UsersnikosDesktopdbnikos.accdb;"
  • 6. 'Create the SQL statement to read the file. Filter all the data from Canada. 'Note that the filename is used instead of the table name. sql = "SELECT * FROM nikos"
  • 7. On Error Resume Next 'Create the ADODB recordset object. Set rs = CreateObject("ADODB.recordset") 'Check if the object was created. If Err.Number <> 0Then MsgBox "Connection was not created!", vbCritical, "Connection error" Exit Sub End If On Error GoTo 0
  • 8. 'Set thee cursor location. rs.CursorLocation = 3 'adUseClient on early binding rs.CursorType = 1 'adOpenKeyset on early binding 'Open the recordset. rs.Open sql, con 'Redim the table that will contain the filtered data. ReDim myValues(rs.RecordCount, 4)
  • 9. 'Loop through the recordset and pass the selected values to the array. i = 1
  • 10. If Not (rs.EOFAnd rs.BOF)Then 'Go to the first record. rs.MoveFirst Do Until rs.EOF =True myValues(i, 1) = rs!onoma myValues(i, 2) = rs!onoma myValues(i, 3) = rs!onoma myValues(i, 4) = rs!onoma 'Move to the next record. rs.MoveNext i = i + 1 Loop Else
  • 11. Else 'Close the recordet and the connection. rs.Close con.Close 'Release the objects. Set rs = Nothing Set con = Nothing 'Enable the screen. Application.ScreenUpdating =True 'In case of an empty recordset display an error. MsgBox "There are no records in the recordset!", vbCritical, "No Records" Exit Sub End If
  • 12. 'Write the array in the sheet. 'Sheet1.Activate For i = 1To UBound(myValues) For j = 1To 4 Cells(i + 1, j) = myValues(i, j) Next j Next i
  • 13. 'Close the recordet and the connection. rs.Close con.Close 'Release the objects. Set rs = Nothing Set con = Nothing
  • 14. 'Adust the columns width. Columns("A:D").EntireColumn.AutoFit 'Enable the screen. Application.ScreenUpdating =True
  • 15. 'Inform the user that the macro was executed successfully. MsgBox "The values were read from recordset successfully!", vbInformation, "Done" End Sub