Usually do the database operation more want to put the content of often use together, I also lazy, in a book of engineering examples picked a bas, fix change, do this use it.
1 PublicStrcnn as String 'Database connection String2 PublicAdocnn asADODB. Connection'Database Connection3 PublicIsconnect as Boolean 'determine if the connection4 5 6 Private SubConnect ()'connecting to a database7 on Error GoToERR:8 IfIsconnect =True Then 'returns if the connection is marked as true. Otherwise, it will go wrong9 Exit SubTen End If One A SetADOCNN =NewADODB. Connection'key new to create new objects CNN - withadocnn -. ConnectionString =strcnn the. ConnectionTimeout =Ten - . Open - End with -Isconnect =True 'Set the connection token to indicate that you are connected to the database + Exit Sub - ERR: + IfERR =-2147467259 Then A SetCnn = Nothing at MsgBoxErr.Description &"Please check the database configuration!", vbOKOnly + vbinformation,"Connect" - Else - MsgBoxErr.Description &"Please check the database configuration!", vbexclamation,"Connect" - End If - - End Sub in - Public SubDisconnect ()'disconnecting from the database to DimRc as Long + IfIsconnect =False Then Exit Sub 'If the connection is marked as false, indicating that the connection has been disconnected, return directly -Adocnn.close'Close Connection the * SetADOCNN = Nothing $Isconnect =FalsePanax Notoginseng End Sub - the Public SubDb_connect ()'using Connect_num to control database connections +Connect_num = Connect_num +1 A Connect the End Sub + - Public SubDb_disconnect () $ IfConnect_num >= Connect_loop_max Then $Connect_num =0 - Disconnect - End If the End Sub - Wuyi Public SubDbapi_disconnect ()'Force shutdown API-mode access to the database, counter reset theConnect_num =0 - Disconnect Wu End Sub - About Public SubExecsql (ByValTmpsql as String)'Execute database Operation statement $ on Error GoToERR: - DimCmd as NewAdodb.command'create Command object cmd -Db_connect'connecting to a database - SetCmd. ActiveConnection = adocnn'set the ActiveConnection property of CMD to specify the database connection with which it is associated ACmd.commandtext = Tmpsql'Set the command text to execute + cmd. Execute the Setcmd = Nothing - Db_disconnect $ Exit Sub the ERR: the MsgBoxErr.Description, -,"Execsql" the End Sub the - Public FunctionQuerysql (ByValTmpsql as String) asADODB. Recordset'Executing database query statements in on Error GoToERR: the DimRst as NewADODB. Recordset theDb_connect'connecting to a database About IfIsconnect =False Then Exit Function the SetRst. ActiveConnection = adocnn'set the ActiveConnection property of the RST to specify the database connection to which it is associated theRst. CursorType =adOpenKeyset theRst. LockType = adLockOptimistic'Setting the lock type +Rst. Open Tmpsql'Open Record Set - SetQuerysql = rst'back to record set the Exit FunctionBayi ERR: the MsgBoxErr.Description, -,"Querysql" the End Function - - Public FunctionGetFieldValue (Fieldvalue asVariant) as String theGetFieldValue =IIf( notIsNull (Fieldvalue), Fieldvalue,"") the End Function