Knowledge learned from Access to VBA development

Source: Internet
Author: User
Tags access database

Using ADO to connect to the database of itself, you need to first create a Adodb.connection connection object
Set cn = CreateObject ("ADODB. Connection ")
The provider of the database CN. Provider = "microsoft.ace.oledb.12.0"
Currentproject.path is the path where the current Access database program is obtained STRCN = "Data Source =" & Currentproject.path & "\accessdatabase.accdb ; Jet oledb:database password= "Open connection
cn. Open (STRCN) creates recorderset to receive the queried data
Set rs = CreateObject ("ADODB". Recordset ")
Need to execute the retrieved SQL text Rs. Open "SELECT * FROM tablename" & WhereCondition, CN, 3, 1 rs.fields () Gets the value of the field in the table, the column header ordinal is starting from 0, in this case I wrote 1, that is, the second column in the table KKN = Rs.fields (1)

Delete the SQL text
Using the connected Execute method, the first argument is the SQL text, the second parameter is the number of deleted records, and if successful returns 1, the failure returns 0
cn. Execute "DELETE from tablename where name = '" & Value & "' and birthday = CDate ('" & Value & "')", CNT

The updated SQL text
The parameters are as above, CNT is the number of records returning the update, 1 is success, 0 is the failure
CN. Execute "UPDATETableNameSET Update field = Updated valueWHERE name = ' "& Value &" ' and birthday = CDate (' "& Value &" ') ", CNT

Inserted SQL text
As above, CNT is the number of record entries returned, 1 is successful, 0 is a failure
cn. Execute "INSERT INTO tablename values (value per field)", CNT

Query the SQL text for count (*)
Because there is only one field after this query, use Rs.fields (0), 1 is already present, and 0 is not present
Rs. Open "Select COUNT (1) from tablename WHERE name = '" & Value & "' and birthday = CDate ('" & Value & "')", CN, 3, 1
cnt = rs.fields (0)

Check whether the checkbox is selected
Me.chk.Value =-1 is selected

IsNull () to determine if NULL
Len () to determine the length of the data

The judgment of the email address

Set Tkexp = CreateObject ("VBScript.RegExp")
Tkexp.pattern = "^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$ "
If tkexp.test (email) = False Then
MsgBox "email format is not correct. ", 48
Me. Email from home. SetFocus
Exit Function
End If

The meaning of the msgbox,33 with the returned result is that the 32+1,32 is a prompt with a question mark icon, and 1 is the "OK" and "Cancel" buttons, so you can add up the numbers they represent when you need to specify the buttons and icons.
When you click Cancel the return value is 2, and the value of Vbcancel is 2, if the return value and vbcancel value is consistent, then you click on the Cancel button, MsgBox will be listed in detail after

Msgret = MsgBox ("msg", 33)
If Msgret = vbcancel Then
Exit Sub
End If

To open a new form, use a separate description
DoCmd.OpenForm "Openedformname",,,,,, "parameters to pass"
The notation received in Openedformname is that the variable =strtransitformname = Me.openargs

The subform invokes the main form's method and closes its own

Dim Main as MainForm
Set main = MainForm
Main. Eiken Cable _click
DoCmd.Close acform, "ChildForm"

Subform Right-click to delete data settings, Design view license set to No

Knowledge learned from Access to VBA development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.