connect with access

Reply

Join Date: Jun 2005
Posts: 5
Reputation: amir_ct2001 is an unknown quantity at this point 
Solved Threads: 0
amir_ct2001 amir_ct2001 is offline Offline
Newbie Poster

connect with access

 
0
  #1
Jun 24th, 2005
how can i connect Vb with access database that have password to login using ADO(OLEDB) connectivity.give codes
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 508
Reputation: techniner is an unknown quantity at this point 
Solved Threads: 19
techniner techniner is offline Offline
Posting Pro

Re: connect with access

 
0
  #2
Jun 24th, 2005
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Command1_Click()
  2.  
  3. 'Define the three objects that we need,
  4. ' A Connection Object - connects to our data source
  5. ' A Command Object - defines what data to get from the data source
  6. ' A RecordSet Object - stores the data we get from our data source
  7.  
  8. Dim conConnection As New ADODB.Connection
  9. Dim cmdCommand As New ADODB.Command
  10. Dim rstRecordSet As New ADODB.Recordset
  11.  
  12.  
  13. 'Defines the connection string for the Connection. Here we have used fields
  14. 'Provider, Data Source and Mode to assign values to the properties
  15. ' conConnection.Provider and conConnection.Mode
  16.  
  17. conConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
  18. App.Path & "\" & "database.mdb;Mode=Read|Write"
  19.  
  20.  
  21. 'Define the location of the cursor engine, in this case we are opening an Access database
  22. 'and adUseClient is our only choice.
  23.  
  24. conConnection.CursorLocation = adUseClient
  25.  
  26.  
  27. 'Opens our connection using the password "Admin" to access the database. If there was no password
  28. 'protection on the database this field could be left out.
  29.  
  30. conConnection.Open
  31.  
  32.  
  33. 'Defines our command object
  34.  
  35. ' .ActiveConnection tells the command to use our newly created command object.
  36. ' .CommandText tells the command how to get the data, in this case the command
  37. ' will evaluate the text as an SQL string and we will return all
  38. ' records from a table called tabTestTable
  39. ' .CommandType tells the command to evaluate the .CommandText property as an SQL string.
  40.  
  41. With cmdCommand
  42. .ActiveConnection = conConnection
  43. .CommandText = "SELECT * FROM tabTestTable;"
  44. .CommandType = adCmdText
  45. End With
  46.  
  47. 'Defines our RecordSet object.
  48.  
  49. ' .CursorType sets a static cursor, the only choice for a client side cursor
  50. ' .CursorLocation sets a client side cursor, the only choice for an Access database
  51. ' .LockType sets an optimistic lock type
  52. ' .Open executes the cmdCommand object against the data source and stores the
  53. ' returned records in our RecordSet object.
  54.  
  55. With rstRecordSet
  56. .CursorType = adOpenStatic
  57. .CursorLocation = adUseClient
  58. .LockType = adLockOptimistic
  59. .Open cmdCommand
  60. End With
  61.  
  62. 'Firstly test to see if any records have been returned, if some have been returned then
  63. 'the .EOF property of the RecordSet will be false, if none have been returned then the
  64. 'property will be true.
  65.  
  66. If rstRecordSet.EOF = False Then
  67.  
  68. 'Move to the first record
  69.  
  70. rstRecordSet.MoveFirst
  71.  
  72. 'Lets move through the records one at a time until we reach the last record
  73. 'and print out the values of each field
  74.  
  75. Do
  76.  
  77. 'Access the field values using the fields collection and print them to a message box.
  78. 'In this case I do not know what you might call the columns in your database so this
  79. 'is the safest way to do it. If I did know the names of the columns in your table
  80. 'and they were called "Column1" and "Column2" I could reference their values using:
  81.  
  82. ' rstRecordSet!Column1
  83. ' rstRecordSet!Column2
  84.  
  85.  
  86. MsgBox "Record " & rstRecordSet.AbsolutePosition & " " & _
  87. rstRecordSet.Fields(0).Name & "=" & rstRecordSet.Fields(0) & " " & _
  88. rstRecordSet.Fields(1).Name & "=" & rstRecordSet.Fields(1)
  89.  
  90. 'Move to the next record
  91.  
  92. rstRecordSet.MoveNext
  93. Loop Until rstRecordSet.EOF = True
  94.  
  95. 'Add a new record
  96.  
  97. With rstRecordSet
  98. .AddNew
  99. .Fields(0) = "New"
  100. .Fields(1) = "Record"
  101. .Update
  102. End With
  103.  
  104. 'Move back to the first record and delete it
  105.  
  106. rstRecordSet.MoveFirst
  107. rstRecordSet.Delete
  108. rstRecordSet.Update
  109.  
  110.  
  111. 'Close the recordset
  112.  
  113. rstRecordSet.Close
  114. Else
  115. MsgBox "No records were returned using the query " & cmdCommand.CommandText
  116. End If
  117.  
  118. 'Close the connection
  119.  
  120. conConnection.Close
  121.  
  122. 'Release your variable references
  123.  
  124. Set conConnection = Nothing
  125. Set cmdCommand = Nothing
  126. Set rstRecordSet = Nothing
  127. End Sub
Last edited by techniner; Jun 24th, 2005 at 9:30 am. Reason: Typo
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: connect with access

 
0
  #3
Jun 28th, 2005
http://www.daniweb.com/techtalkforums/thread26892.html is a great tutorial over using access with VB.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC