Đố vui về ADO, DAO.

Liên hệ QC
Tiếp đến tôi có câu đố như sau:
Tôi có file rỗng A.xlsx

1595569905891.png

File B.xlsm chứa code, file này có dữ liệu như sau:

1595570010794.png

2 file này chung 1 thư mục. Hỏi làm sao ta không dùng vòng lặp mà có thể ghi toàn bộ dữ liệu từ Sheet1 của file B.xlsm, với 1 sheet mới có tên là DuLieuTu_Sheet1.

Kết quả file A.xlsx

1595570496412.png
 

File đính kèm

  • DoVui.rar
    11.8 KB · Đọc: 9
(@$%@ Em thông minh theo ngày, hôm nay cuối tuần nên Pó tay hihi
ua mà cái này có điều kiện gì nữa không anh.
Copy sheet
copy range
........
Không em, chỉ dùng chuỗi kết nối rồi sau đó tạo bảng, thêm cột, chèn dữ liệu vào.
 
Tức là dùng ADO đó hả anh.

Nếu vậy quá đơn giản, anh phải thêm cái ĐK là giữ nguyên mọi định dạng, công thức cơ
 
Lần chỉnh sửa cuối:
Tiếp đến tôi có câu đố như sau:
Tôi có file rỗng A.xlsx

View attachment 241731

File B.xlsm chứa code, file này có dữ liệu như sau:

View attachment 241735

2 file này chung 1 thư mục. Hỏi làm sao ta không dùng vòng lặp mà có thể ghi toàn bộ dữ liệu từ Sheet1 của file B.xlsm, với 1 sheet mới có tên là DuLieuTu_Sheet1.

Kết quả file A.xlsx

View attachment 241737

đoán nhé ... xóa File A đi xong copy chính File đó sang xong đổi tên thành File A
 
Úp file File *.PDF này lên đây cho các Bạn làm bài #401 ... Mạnh tới khúc này xem như xong -0-0-0-
 

File đính kèm

  • ADO.rar
    66.5 KB · Đọc: 16
Tạo Sheet Mới đại ý nó như sau
Mã:
est "CREATE TABLE Persons (PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255))"
vậy cơ bản xong 50% rồi đó he
Thật ra câu đố này không mới, bởi vì ở đề tài này chưa có nên muốn đưa vào cho mọi người tham khảo thêm.
 
Thật ra câu đố này không mới, bởi vì ở đề tài này chưa có nên muốn đưa vào cho mọi người tham khảo thêm.
Cơ bản Tây nó viết như sau ... còn vận dụng sao hay chế biến sao tùy vào khả năng và sở thích của từng Bạn
Mạnh úp lên cho các Bạn trẻ tham khảo 1 sub cơ bản
Mã:
Sub Test(sSQL As String)
    Dim oCN As Object
    Set oCN = CreateObject("ADODB.Connection")
    oCN.Open "ODBC;DSN=Excel Files;" & "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};" & "DBQ=D:\22.xlsx;" & "ReadOnly=0;"
    oCN.Execute sSQL
    oCN.Close
    Set oCN = Nothing
 End Sub
Sử dụng như sau
Mã:
Sub Main_Test()
    Test "CREATE TABLE Persons (PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255))"
End Sub

Cách sử dụng khác
Mã:
We will use this routine to execute SQL statements against XL and see what it does and doesn't support. Start by pressing CTRL-G to get to Windows immediately. Copy this to the window immediately.
Test "CREATE TABLE Persons (PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255))"
Click to the right of the line and press ENTER. That created a M?i Workbooktrong C: \ du?c g?i là Test.xls và dã thêm m?t B?ng tính có tên Ngu?i v?i nam tiêu d? c?t (Ð?t cu?c b?n không bi?t VBA c?a XL có th? làm du?c!) XL dã th?c hi?n chính xác nh?ng gì DBMS nên làm. Vì v?y, hãy xem nh?ng gì XL khác có th? làm nhu m?t DBMS. Hãy th? dòng này:

Test "CREATE TABLE States (Code varchar, Description varchar)"
That added another Worksheet to Test.xls called States with two column headings. Wow - XL looks like a database - right? Let's see. Try this:

Test "INSERT INTO Persons VALUES (1,'Hatmaker','Craig','100 Main St', 'Anytown')"
That has added a record to the People spreadsheet - just like a database. Please continue. Type:

Test "DELETE FROM Persons Where PersonID = 1"
That happens with the " Data type doesn't match in criteria expression" l?i. Huh? The error says that XL thinks PersonID is no longer a number. See if it thinks it is a character. Type:

Test "DELETE FROM Persons Where PersonID = '1'"
XL reports " Delete data in the linked table not supported by this ISAM ", which means that XL simply does not support basic database operations like DELETE. But hey! We know we can delete records in XL. Give what? And to be honest, I have no idea. All I know is that SQL is the standard for database manipulation, but XL refuses to let SQL perform this basic operation. Go on. To try on:

Test "ALTER TABLE Persons ADD DateOfBirth date"
This is supposed to add a column. XL reports "Invalid activity ". Again, we can do that through the XL's GUI but XL rejects SQL.

Test "CREATE INDEX PersonsByName ON Persons (PersonID)"
XL reports "Unsupported operation for this type of audience ". An Index provides a code-based, somewhat similar approach to the INDEX function INDEX . Nhung cung gi?ng nhu b?t k? ch?c nang, cácINDEX ch?c nang of XL returns the result. An Index co s? d? li?u nào not. It is an object that holds a pointer to all records (not just one) sorted by speed based on the criteria in the  CREATE INDEX statement . There is no Index v?t in XL and thus this SQL operation fails.

Test "CREATE VIEW PersonsByName as SELECT LastName FROM Persons"
This is supposed to create an object that only displays the LastNamecánh d?ng. Think of this as hiding all columns except LastName . This is useful for security but fails with the message " Operation not supported for this type of object ". And think about security:

Test "GRANT SELECT ON Persons TO [Craig Hatmaker]"
This is supposed to grant me authorization for the People table. XL reports invalid SQL Statement; Expect 'DELETE', 'CONFIRM', 'PROCEDURE', 'SELECT' or 'UPDATE' unlike DBMS, XL does not have user-based security.

Explore this strange territory alone. Try any SQL statement using routine check and discover exactly what XL can and can not do as a DBMS ..

Vậy là đồ chơi có hết rồi .... thêm hành + tỏi + nước mắm + dầu ăn nữa là thơm thôi
 
Cơ bản Tây nó viết như sau ... còn vận dụng sao hay chế biến sao tùy vào khả năng và sở thích của từng Bạn
Mạnh úp lên cho các Bạn trẻ tham khảo 1 sub cơ bản
Mã:
Sub Test(sSQL As String)
    Dim oCN As Object
    Set oCN = CreateObject("ADODB.Connection")
    oCN.Open "ODBC;DSN=Excel Files;" & "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};" & "DBQ=D:\22.xlsx;" & "ReadOnly=0;"
    oCN.Execute sSQL
    oCN.Close
    Set oCN = Nothing
End Sub
Sử dụng như sau
Mã:
Sub Main_Test()
    Test "CREATE TABLE Persons (PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255))"
End Sub

Cách sử dụng khác
Mã:
We will use this routine to execute SQL statements against XL and see what it does and doesn't support. Start by pressing CTRL-G to get to Windows immediately. Copy this to the window immediately.
Test "CREATE TABLE Persons (PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255))"
Click to the right of the line and press ENTER. That created a M?i Workbooktrong C: \ du?c g?i là Test.xls và dã thêm m?t B?ng tính có tên Ngu?i v?i nam tiêu d? c?t (Ð?t cu?c b?n không bi?t VBA c?a XL có th? làm du?c!) XL dã th?c hi?n chính xác nh?ng gì DBMS nên làm. Vì v?y, hãy xem nh?ng gì XL khác có th? làm nhu m?t DBMS. Hãy th? dòng này:

Test "CREATE TABLE States (Code varchar, Description varchar)"
That added another Worksheet to Test.xls called States with two column headings. Wow - XL looks like a database - right? Let's see. Try this:

Test "INSERT INTO Persons VALUES (1,'Hatmaker','Craig','100 Main St', 'Anytown')"
That has added a record to the People spreadsheet - just like a database. Please continue. Type:

Test "DELETE FROM Persons Where PersonID = 1"
That happens with the " Data type doesn't match in criteria expression" l?i. Huh? The error says that XL thinks PersonID is no longer a number. See if it thinks it is a character. Type:

Test "DELETE FROM Persons Where PersonID = '1'"
XL reports " Delete data in the linked table not supported by this ISAM ", which means that XL simply does not support basic database operations like DELETE. But hey! We know we can delete records in XL. Give what? And to be honest, I have no idea. All I know is that SQL is the standard for database manipulation, but XL refuses to let SQL perform this basic operation. Go on. To try on:

Test "ALTER TABLE Persons ADD DateOfBirth date"
This is supposed to add a column. XL reports "Invalid activity ". Again, we can do that through the XL's GUI but XL rejects SQL.

Test "CREATE INDEX PersonsByName ON Persons (PersonID)"
XL reports "Unsupported operation for this type of audience ". An Index provides a code-based, somewhat similar approach to the INDEX function INDEX . Nhung cung gi?ng nhu b?t k? ch?c nang, cácINDEX ch?c nang of XL returns the result. An Index co s? d? li?u nào not. It is an object that holds a pointer to all records (not just one) sorted by speed based on the criteria in the  CREATE INDEX statement . There is no Index v?t in XL and thus this SQL operation fails.

Test "CREATE VIEW PersonsByName as SELECT LastName FROM Persons"
This is supposed to create an object that only displays the LastNamecánh d?ng. Think of this as hiding all columns except LastName . This is useful for security but fails with the message " Operation not supported for this type of object ". And think about security:

Test "GRANT SELECT ON Persons TO [Craig Hatmaker]"
This is supposed to grant me authorization for the People table. XL reports invalid SQL Statement; Expect 'DELETE', 'CONFIRM', 'PROCEDURE', 'SELECT' or 'UPDATE' unlike DBMS, XL does not have user-based security.

Explore this strange territory alone. Try any SQL statement using routine check and discover exactly what XL can and can not do as a DBMS ..

Vậy là đồ chơi có hết rồi .... thêm hành + tỏi + nước mắm + dầu ăn nữa là thơm thôi
Anh lưu ý là những câu lệnh trên không hỗ trợ hết cho Excel nhé.

oCN.Open "ODBC;DSN=Excel Files;" & "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};" & "DBQ=D:\22.xlsx;" & "ReadOnly=0;"
Xin lưu ý thêm: Có lần nói anh về cái chuỗi kết nối kiểu này nó sẽ không hỗ trợ kiểu UTF-8 nhé.
 
Lần chỉnh sửa cuối:
Web KT
Back
Top Bottom