这个系列两年后卷土重来了…
最近在用ms access, access中也可以使用sql,但跟其他sql语法不大一样(低情商:简陋).
于是记录access中的sql使用, 亦或是 用vba绕过sql的使用.
Notes
以下最常见在vba中的sql使用.
OpenRecordSet
1 | Dim sSQL As String |
如果不需要select的情况可以直接开table.
1 | Dim rs As DAO.Recordset |
1 | Dim Oapple As Object |
这里table的value可以有很多种,table中甚至可以store文件.
OpenRecordSet之后如何使用其value: 使用了getRows.
注意,
1 | Dim theName As String |
可以这样用variable.
1 | Set Oapple = rs.Fields!theName.Value |
这时候theName必须生死rs中已有的column的名字,不可以用variable.
Using Variable on accessing the Form and Controls in MS Access.
Form中也类似.
1 | Set theFields As String |
这样用.
Execute
不需要store 信息时候使用db,也就是execute.
比如update, delete的function.
1 | Dim sSQL As String |
1 | Dim db As DAO.Database |
Function
How to return a result from a VBA function.
VBA有两种function,一种key word 是sub,一种key word是function.
Listbox
这是access form中的信息display.
1 | sSQL = "SELECT fruits FROM tableA;" |
vbCrLf
Differences Between vbLf, vbCrLf & vbCr Constants.
Constant | Value | Description |
---|---|---|
vbCr | Chr(13) | Carriage return(返回到行首) |
vbCrLf | Chr(13) & Chr(10) | Carriage return–linefeed combination(类似于按回车键) |
vbLf | Chr(10) | Line feed(转到下一行) |
Cases
SQL join against date ranges
这可真是个好问题,我觉得stack over flow的答案写的很好.
但在access 里面实现太麻烦了…
1 | --先用order和self join作出一个end_date的column |
Form load
Form load中的代码会over write在design view中各compent的default value.
Form的跳转的觉得下面这个link说的有道理.
How to call Form_Load of one form from another form in Access VBA
做一个public的function然后call public就好了.Form load有的时候没办法handle从哪个form跳转而来的问题.
Add index
1 | ALTER TABLE fruits ADD COLUMN ID COUNTER; |
On Error Resume Next
才直到这个是一个switch,这行代码会让之后出现的所有error都被跳过.
超级危险,不要用.
Export data
TableName:Variant A string expression that is the name of the Office Access table that you want to import spreadsheet data into, export spreadsheet data from, or link spreadsheet data to, or the Access select query whose results you want to export to a spreadsheet.
用query来export.
1 | Dim MyQuery As DAO.QueryDef |
我觉得这个地方documentation写的“query也行”不是SQL string,而是真的得是define好的query.
比如在access页面也能create query.
用define好的query名字也行.
Create后的delete.
用access中的table来export.
1 | Dim MyQuery As DAO.QueryDef |
关于如何用代码来修改table内容.
可以用OpenRecordset.
1 | Dim myInput, myOutput As DAO.Recordset |
这里的DoEvents是用来让break button active的(据说).
上面使用了EOF,microsoft给的例子是:
1 | If myInput.EOF = True Or myInput.BOF = True Then |
我觉得这样写好(?).
Default value
(For excel as well)
Data is stored as number, so when setting default values for date can use number format instead of any long/short data format cuz somethings I have no idea why it’s not working(怒).
Btw for excel when a sheet is hidden… and the tab is locked for unhidden sheet, use alt+F11 to go to VBA module, can find the sheet name, then go back to sheet using reference to display the value anyway(暴怒).
Delete table
1 | DoCmd.DeleteObject acTable, "Former Employees Table" |
1 |
|
Open file
根据Application.FileDialog property.
Name | Value | Description |
---|---|---|
msoFileDialogFilePicker | 3 | File Picker dialog box. |
msoFileDialogFolderPicker | 4 | Folder Picker dialog box. |
msoFileDialogOpen | 1 | Open dialog box. |
msoFileDialogSaveAs | 2 | Save As dialog box. |
所以选中文件是f = Application.FileDialog(3).
1 | Dim f As Object |
Browse for file and get file name and path
1 | '在给f路径后,用f.show来打开folder. |
Import excel data
Excel 8.0: 97-2003 xls files
Excel 12.0: xlsb files
Excel 12.0 Xml: xlsx files
1 | Dim db As DAO.Database |
老哥的代码,其中我觉得(…)是这样的.
[Excel 12.0 Xml;HDR=YES;IMEX=2;ACCDB=YES;DATABASE=C:\Links\Products.xlsx].[Sheet1$]就可以当作一个table用了.
My version:
1 | Select * INTO newTable from [Excel 12.0 Xml;HDR=YES;IMEX=2;ACCDB=YES;DATABASE=C:\Links\Products.xlsx].[Sheet1$]; |
是一种开盲盒的select all大法.
Import when select..<-傻子找到了但没有测.
傻子下次要import when select的时候再来补.
Object
使用With更改more than one field.
在database中的使用.
类似的在recordset中的使用.
1 | Dim rstCategories As Recordset |
在with中更改record set.
How to List Field’s Name in table in Access Using SQL
遇到了盲盒case.
要import excel data到access中但import的table会是什么样完全不知道.
于是依靠access自己去读column name,并且用resize的array来store column name.
根据两位老哥的代码改了一改.
1 | Sub ListFields() |
How to check if a table exists in MS Access for vb macros
用DCount.
1 | Public Function ifTableExists(tblName As String) As Boolean |