Code Snippets

  

Visual Basic Source Code


Welcome to Dream.In.Code
Become a VB Expert!

Join 137,218 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,221 people online right now. Registration is fast and FREE... Join Now!





Database Connection

Simple function to connect to a database which can be used in the entire project

Submitted By: nealgabriel45
Actions:
Rating:
Views: 4,440

Language: Visual Basic

Last Modified: February 11, 2008
Instructions: Create a class; Copy the First 4 function listed;
Create a module to create the object for the class;
Call the class_initialize and InitializeConnection in the submain function or the function / form where program starts [This is to make sure that the connectivity is made possible];
Terminate the connection before the program teminates / end

Simple Ah ?

Note: The Connection String varies according to the database used. Wait for another simple function which supports multiple db connection without changing the actual connection string in the program.

Snippet


  1. 'Code to be written in a Class [clsData is the class name]
  2. Public m_dbConn As ADODB.Connection
  3. Public m_dbCmd As ADODB.Command
  4. 'Database Connection - Connection String
  5. Public Function InitializeConnection() As Boolean
  6.    On Error GoTo Error_Handler
  7.    Dim strWinPath As String
  8.    strWinPath = StripNulls(szBuffer)
  9.    Dim strConnection As String
  10.    m_dbConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Database\Master.mdb;Persist Security Info=False;Jet OLEDB:Database Password='samtron'"
  11.    m_dbCmd.ActiveConnection = m_dbConn
  12.    InitializeConnection = True
  13.    Exit Function
  14.    
  15. Error_Handler:
  16.    InitializeConnection = False
  17. End Function
  18.  
  19. Public Function DoQuery(strQuery As String) As Boolean
  20. 'Command Object is being Initialized
  21.    On Error GoTo Error_Handler
  22.    
  23.    With m_dbCmd
  24.       .CommandType = adCmdText
  25.       .CommandText = strQuery
  26.       .Execute
  27.    End With
  28.    
  29.    DoQuery = True
  30.    Exit Function
  31.  
  32. Error_Handler:
  33.    DoQuery = False
  34. End Function
  35.  
  36. Private Sub Class_Initialize()
  37.    Set m_dbConn = New ADODB.Connection
  38.    Set m_dbCmd = New ADODB.Command
  39. End Sub
  40.  
  41. Private Sub Class_Terminate()
  42.    If m_dbConn.State = adStateOpen Then
  43.       m_dbConn.Close
  44.    End If
  45.  
  46.    Set m_dbConn = Nothing
  47.    Set m_dbCmd = Nothing
  48. End Sub
  49.  
  50. 'Code to be written in  a module
  51. Public objData as new clsData 'Where clsData is the class name
  52.  
  53. 'Function call in the forms / project
  54. Dim strQuery as String
  55. strQuery = "Some Valid SQL Query"
  56.  
  57. If Not g_objData.DoQuery(strQuery) Then
  58.    Msgbox "Data Not Updated" 'Some Message
  59. else
  60.    Msgbox "Data Updated"
  61. end if
  62.  

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month