Home Database Mysql Tutorial VB操作access数据库

VB操作access数据库

Jun 07, 2016 pm 03:43 PM
access operate data database connect

第一次用VB来连接Access数据库,由于不了解VB语法,老是出现bug。现在看来,其实很简单,有好几种方法来连接,包括ADO控件、利用ADO对象等方法。由于ADO控件操作局限性太大,本文采用ADO对象来操作。过程如下: 准备工作: 建立数据库 E:\operation.accdb;

第一次用VB来连接Access数据库,由于不了解VB语法,老是出现bug。现在看来,其实很简单,有好几种方法来连接,包括ADO控件、利用ADO对象等方法。由于ADO控件操作局限性太大,本文采用ADO对象来操作。过程如下:


准备工作: 建立数据库 E:\operation.accdb; 建数据表:selection;数据表中,建字段:User,Password,并添加一条记录

点工程->引用 选中Microsoft Activex Data Objects 6.1 Library(注:选择最高版本就行)



1、数据库查询

首先添加控件:添加一个文本框,一个命令按钮

Private Sub Command1_Click()
Dim Conn As New ADODB.Connection '创建ADODB对象,用于连接数据库操作
Dim Rs As New ADODB.Recordset  '创建ADODB对象,用于返回取得的数据库内容
Conn.Open "Provider=microsoft.ace.oledb.12.0;Data Source=E:\operation.accdb"  '连接数据库函数,第一个参数为数据库驱动,第二个参数为数据库路径

sql = "Select Password From select where User='a'" 
'selection请换成你的实际数据表名
Rs.Open sql, Conn, 1, 3 '查询数据库,第一个参数为数据库查询语句,第二个参数为数据库连接,第三个和第四个分别为对数据库的操作权限;
If Rs.EOF Then
MsgBox "没有找到此用户"
Else
Text1.Text = Rs("Password")
End If
Conn.close
Rs.close
End Sub
Copy after login

2、数据插入

首先添加控件:添加两个文本框,一个命令按钮

Private Sub Command1_Click()
Dim s1 As String
Dim s2 As String
Dim Conn As New ADODB.Connection
Conn.Open "Provider=microsoft.ace.oledb.12.0;Data Source=E:\operation.accdb"

s1 = Text1.Text
s2 = Text2.Text
sql = "Insert Into selection Values('" & s1 & "','" & s2 & "')"
Conn.Execute sql
Conn.close
End Sub
Copy after login

3、删除数据库中的某个记录

首先添加控件:添加一个文本框,一个命令按钮

Private Sub Command1_Click()
Dim s As String
Dim Conn As New ADODB.Connection
Conn.Open "Provider=microsoft.ace.oledb.12.0;Data Source=E:\operation.accdb"

s = Text1.Text
sql = "Delete From selection Where User = '" & s & "'"
Conn.Execute sql
Conn.close
End Sub
Copy after login



4、更新数据库某个记录

首先添加控件:添加一个文本框,一个命令按钮


Private Sub Command1_Click()
Dim s As String
Dim Conn As New ADODB.Connection
Conn.Open "Provider=microsoft.ace.oledb.12.0;Data Source=E:\operation.accdb"
s = Text1.Text
sql = "Update selection Set [Password] = '" & s & "' Where [User] = 'a'"

Conn.Execute sql
Conn.Close
End Sub
Copy after login


总结

首先是建立数据库连接:

Private Sub Command1_Click()
Dim Conn As New ADODB.Connection '创建ADODB对象,用于连接数据库操作
Dim Rs As New ADODB.Recordset  '创建ADODB对象,用于返回取得的数据库内容
Conn.Open "Provider=microsoft.ace.oledb.12.0;Data Source=E:\operation.accdb"  '连接数据库函数,第一个参数为数据库驱动,第二个参数为数据库路径
Copy after login
然后,是对数据库各种操作语句




Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to configure zend for apache How to configure zend for apache Apr 13, 2025 pm 12:57 PM

How to configure Zend in Apache? The steps to configure Zend Framework in an Apache Web Server are as follows: Install Zend Framework and extract it into the Web Server directory. Create a .htaccess file. Create the Zend application directory and add the index.php file. Configure the Zend application (application.ini). Restart the Apache Web server.

How to use Debian Apache logs to improve website performance How to use Debian Apache logs to improve website performance Apr 12, 2025 pm 11:36 PM

This article will explain how to improve website performance by analyzing Apache logs under the Debian system. 1. Log Analysis Basics Apache log records the detailed information of all HTTP requests, including IP address, timestamp, request URL, HTTP method and response code. In Debian systems, these logs are usually located in the /var/log/apache2/access.log and /var/log/apache2/error.log directories. Understanding the log structure is the first step in effective analysis. 2. Log analysis tool You can use a variety of tools to analyze Apache logs: Command line tools: grep, awk, sed and other command line tools.

What is apache server? What is apache server for? What is apache server? What is apache server for? Apr 13, 2025 am 11:57 AM

Apache server is a powerful web server software that acts as a bridge between browsers and website servers. 1. It handles HTTP requests and returns web page content based on requests; 2. Modular design allows extended functions, such as support for SSL encryption and dynamic web pages; 3. Configuration files (such as virtual host configurations) need to be carefully set to avoid security vulnerabilities, and optimize performance parameters, such as thread count and timeout time, in order to build high-performance and secure web applications.

How to monitor Nginx SSL performance on Debian How to monitor Nginx SSL performance on Debian Apr 12, 2025 pm 10:18 PM

This article describes how to effectively monitor the SSL performance of Nginx servers on Debian systems. We will use NginxExporter to export Nginx status data to Prometheus and then visually display it through Grafana. Step 1: Configuring Nginx First, we need to enable the stub_status module in the Nginx configuration file to obtain the status information of Nginx. Add the following snippet in your Nginx configuration file (usually located in /etc/nginx/nginx.conf or its include file): location/nginx_status{stub_status

Using Dicr/Yii2-Google to integrate Google API in YII2 Using Dicr/Yii2-Google to integrate Google API in YII2 Apr 18, 2025 am 11:54 AM

VprocesserazrabotkiveB-enclosed, Мнепришлостольностьсясзадачейтерациигооглапидляпапакробоглесхетсigootrive. LEAVALLYSUMBALLANCEFRIABLANCEFAUMDOPTOMATIFICATION, ČtookazaLovnetakProsto, Kakaožidal.Posenesko

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

See all articles