Trong VBA Access, đối tượng DoCmd được dùng rất nhiều để điều khiển form, report, query, macro, … giống như bạn “ra lệnh” cho Access làm việc. Mình liệt kê một số nhóm lệnh DoCmd thường dùng nhé :
DoCmd.OpenForm "TênForm" – Mở Form.
DoCmd.Close acForm, "TênForm" – Đóng Form.
DoCmd.OpenReport "TênReport", acViewPreview – Mở Report (xem trước).
DoCmd.Close acReport, "TênReport" – Đóng Report.
DoCmd.OpenQuery "TênQuery" – Mở Query.
DoCmd.OpenTable "TênTable" – Mở Table.
DoCmd.Close – Đóng đối tượng hiện tại.
DoCmd.GoToRecord , , acNext – Chuyển sang bản ghi kế tiếp.
DoCmd.GoToRecord , , acPrevious – Quay về bản ghi trước.
DoCmd.GoToRecord , , acNewRec – Tạo bản ghi mới.
DoCmd.GoToControl "TênControl" – Di chuyển con trỏ tới control.
DoCmd.RunSQL "DELETE FROM ..." – Thực thi lệnh SQL (INSERT, UPDATE, DELETE).
DoCmd.TransferText – Xuất/Nhập dữ liệu văn bản (CSV, TXT).
DoCmd.TransferSpreadsheet – Xuất/Nhập Excel.
DoCmd.TransferDatabase – Kết nối/nhập dữ liệu từ database khác.
DoCmd.PrintOut – In đối tượng hiện tại.
DoCmd.OutputTo acOutputReport, "TênReport", acFormatPDF, "D:\Report.pdf" – Xuất Report ra PDF.
DoCmd.RunMacro "TênMacro" – Chạy macro.
DoCmd.RunCommand acCmdSaveRecord – Lưu bản ghi hiện tại.
DoCmd.RunCommand acCmdCopy – Copy dữ liệu đang chọn.
DoCmd.RunCommand acCmdPaste – Paste dữ liệu.
🔹 Một số lệnh DoCmd.RunCommand thường dùng:
DoCmd.RunCommand acCmdSaveRecord – Lưu bản ghi hiện tại.
DoCmd.RunCommand acCmdDeleteRecord – Xóa bản ghi hiện tại.
DoCmd.RunCommand acCmdUndo – Hoàn tác thay đổi trong bản ghi.
DoCmd.RunCommand acCmdSelectRecord – Chọn bản ghi hiện tại.
DoCmd.RunCommand acCmdCopy – Sao chép dữ liệu.
DoCmd.RunCommand acCmdPaste – Dán dữ liệu.
DoCmd.RunCommand acCmdCloseWindow – Đóng cửa sổ hiện tại.
DoCmd.RunCommand acCmdSaveAsForm – Lưu Form.
DoCmd.RunCommand acCmdRefresh – Làm mới dữ liệu Form.
DoCmd.RunCommand acCmdPrint – In đối tượng hiện tại.
DoCmd.RunCommand acCmdSortAscending – Sắp xếp tăng dần.
DoCmd.RunCommand acCmdSortDescending – Sắp xếp giảm dần.
DoCmd.RunCommand acCmdFilterBySelection – Lọc theo dữ liệu đang chọn.
DoCmd.RunCommand acCmdRemoveFilterSort – Bỏ lọc / sắp xếp.
DoCmd.RunCommand acCmdSpelling – Kiểm tra chính tả.
DoCmd.RunCommand acCmdZoomBox – Phóng to hộp nhập liệu.
DoCmd.RunCommand acCmdFind – Tìm kiếm dữ liệu.
DoCmd.RunCommand acCmdReplace – Thay thế dữ liệu.
Tìm kiếm:
Hướng dẫn chi tiết lệnh DoCmd trong VBA Access, ứng dụng hi làm phần mềm như thế nào ?