Advertisement

01.13.2006 at 10:38AM PST, ID: 21695840
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

VBA: How to lookup/update Access database thru Excel Userform?
Tags: vba, excel, access, database, update
I'm new to this forum and this is my 1st time to use VBA....
I've looked thru the forum and I found that the someone has answered similar question before but it only has the code posted . So I can't visualize how it's gonna work w/o the file
http://www.experts-exchange.com/Applications/MS_Office/Excel/Q_21188706.html?qid=21188706

My Problem:
I've created a userform so the user can open up the form to enter the info on the form and post on a worksheet. After that, the user hits the export button and all the info will be transferred to Access database table. My question is how to update/search the Access record by using the same or separate userform in Excel?  

Below is part of the code in my userform

Private Sub LoadData()
Dim dbmain As ADODB.Connection
Dim rcset As ADODB.Recordset
Dim sqlstr As String
Dim dbPath As String

dbPath = "C:\Documents and Settings\yuenk\Desktop\GAAP Testing\DB1.mdb"

Set dbmain = New ADODB.Connection
dbmain.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath


sqlstr = "SELECT Value " & _
        "FROM Testing " & _
        "WHERE testing.[AnalystName] ='" & Me.cboAnalystName & "';"

"I WOULD LIKE TO PUT MORE CRITERIA IN THE WHERE STATEMENT TO MAKE SURE IT ONLY RETURNS ONE VALUE BUT DON'T KNOW HOW"

Set rcset = New ADODB.Recordset
rcset.Open sqlstr, dbmain, adOpenKeyset, adLockOptimistic

Do Until rcset.EOF
With Me.LstData
.AddItem rcset.Fields(0)
.Column(1, .ListCount - 1) = rcset.Fields(1)
End With
rcset.MoveNext
Loop
ExitSub:
If Not rcset Is Nothing Then rcset.Close: Set rcset = Nothing
If Not dbmain Is Nothing Then dbmain.Close: Set dbmain = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Number & "-" & Err.Description, vbOKOnly + vbExclamation, "Error"
Resume ExitSub
End Sub

Private Sub lstData_Click()
Me.CmdExit.Enabled = Me.LstData.ListIndex > -1
End Sub

-----END CODE----
I know that I have to add more command buttons such as "Search" and "Update" later on.
This piece of code was copied and modified fromt the link above"

I can e-mail my file to you if I don't explan it clearly and  any comments are welcome.

Patrick
Start your free trial to view this solution
Question Stats
Zone: Software
Question Asked By: yuen338
Solution Provided By: Kelvin81
Participating Experts: 4
Solution Grade: B
Views: 640
Translate:
Loading Advertisement...
01.13.2006 at 10:47AM PST, ID: 15694166

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.13.2006 at 11:57AM PST, ID: 15695007

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.13.2006 at 12:14PM PST, ID: 15695189

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.13.2006 at 12:17PM PST, ID: 15695230

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.13.2006 at 12:38PM PST, ID: 15695406

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.13.2006 at 01:58PM PST, ID: 15696076

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.13.2006 at 03:10PM PST, ID: 15696633

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.13.2006 at 03:18PM PST, ID: 15696723

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.13.2006 at 03:24PM PST, ID: 15696784

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.13.2006 at 03:31PM PST, ID: 15696865

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.17.2006 at 06:15AM PST, ID: 15719347

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
01.13.2006 at 10:47AM PST, ID: 15694166

Rank: Guru

Patrick,

I believe that the following URL guide You to a site where SQL are explained in a good manner:
w3Schools: http://www.w3schools.com/sql/sql_and_or.asp

Kind regards,
Dennis



 
01.13.2006 at 11:57AM PST, ID: 15695007
Dennis:

Thanks for your reply.  But are the SQL Select statements in VBA different from Access SQL?
I know how to setup a Select  and And statement in Access but not in VBA.
 
01.13.2006 at 12:14PM PST, ID: 15695189

Rank: Genius

Patrick,

The following does not address you specific situation but it does give some ADODB examples using VBA.

The following text and sample code illustrates how to:

   -> open a database connection
   -> use a stored procedure to perform a query
   -> open a recordset using a custom query
   -> open a recordset using a table name
   -> check for an empty recordset
   -> read all records in a recordset
   -> add a record to a recordset
   -> delete a record from a recordset
   -> copy a recordset with headers to a worksheet
   -> close a recordset and database

This sample code, except for Open database method, can be used with any database such as Access, SQL Server, or Oracle. When using a database, most interaction happens via a recordset. Data is manipulated almost entirely using Recordset objects. Any number of Recordset objects can be created and used at the same time - each representing a different query or the same query. Different Recordset objects can access the same tables, queries, and fields without conflicting.

After opening a Recordset the Recordset can contain zero or more records. One record in the Recordset is always the current record except when the Recordset BOF or EOF property is true in which case no record is the current record. The current record is the record that is affected by any record-specific methods. To move amongst the records in a Recordset use the MoveNext, MovePrevious, MoveLast, and MoveFirst Recordset methods. A specific record can be made the current record by setting the AbsolutePosition property to the index number of the desired record. Fields in the current record are access as follows:

   Value = MyRecordset!Field1
   MyRecordset!Field2 = Value + 1

When the current record is changed use the Update Recordset method to apply the changes to the database. Use the Add method to add a new record and teh Delete method to delete the current record.

Before writing any ADODB code the following references need to be referenced in the VBA project (Tools->References):

   Microsoft ActiveX Data Objects x.x Library
   Microsoft ActiveX Data Objects Recordset x.x Library

For additional information on the ADODB interface see the MSDN pages at:

   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdcstdatatypeenum.asp

Sample code:

   Dim MyDatabase As ADODB.Connection
   Dim MyCommand As New ADODB.Command
   Dim MyRecordset As New ADODB.RecordSet
   Dim Column As Long

   ' Open database connection
   Set MyDatabase = New ADODB.Connection
   MyDatabase.CursorLocation = adUseClient
   MyDatabase.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source='C:\full\path\to\database.mdb'; User Id=admin; Password=;"
   ' For more information about Open syntax:
   '   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmscadomethods.asp
   '   and navigate to Open Method (ADO Connection)
   '   Additional help constucting connection strings can be found at http://www.connectionstrings.com/

   ' Query database using stored procedure (requires command object)
   Set MyCommand.ActiveConnection = MyDatabase
   MyCommand.CommandText = "qrySomeQuery" ' <- name of procedure
   MyCommand.CommandType = adCmdStoredProc
   With MyCommand
      .Parameters.Refresh
      .Parameters.Append .CreateParameter("QueryTextParam", adVarChar, adParamInput, 10, "Value")
      .Parameters.Append .CreateParameter("QueryLongParam", adBigInt, adParamInput, , LongValue)
      .Parameters.Append .CreateParameter("QueryDateParam", adDate, adParamInput, , DateValue)
      .Parameters.Append .CreateParameter("QueryDateTimeStampParam", adDBTimeStamp, adParamInput, , DateTimeValue)
      .Parameters.Append .CreateParameter("BooleanParam", adBoolean, adParamInput, , BooleanValue)
      ' For more information about CreateParameter syntax:
      '   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmscadomethods.asp
      '   and navigate to CreateParameter Method
   End With
   
   ' Open recordset using command onject
   MyRecordset.Open MyCommand, , adOpenDynamic, adLockPessimistic
   ' For more information about Open method syntax:
   '   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmscadomethods.asp
   '   and navigate to Open Method (ADO Recordset)
   
   ' Build custom query using command object
   With MyCommand
      Set .ActiveConnection = Database
      .CommandType = adCmdText
      .CommandText = "SELECT * From tblMyTable WHERE (tblMyTable.MyID = 1)"
   End With
   MyRecordSet.Open MyCommand, , adOpenDynamic, adLockReadOnly

   ' Open recordset by specifying specific table (no query)
   MyRecordset.Open "TableName", MyDatabase, adOpenDynamic, adLockPessimistic
   
   ' Open recordset by specifying query without using command object
   MyRecordset.Open "SELECT * FROM MyTable", MyDatabase, adOpenDynamic, adLockPessimistic

   ' Test for no records
   If MyRecordset.BOF And MyRecordset.EOF Then
      MsgBox "No records in table"
   End If

   ' Determine total records
   MsgBox "Total records: " & MyRecordset.RecordCount
   
   ' Look at all records in record set
   While Not MyRecordset.EOF
      MsgBox "Record number: " & MyRecordset.AbsolutePosition
      MyRecordset.MoveNext
   Wend
   
   ' Update current record
   MyRecordset!Field1 = "Some data"
   MyRecordset!Field2 = "Some more data"
   MyRecordset.Update

   ' Move specific fields from current record to worksheet
   With Sheets("Sheet1")
      Cells(Row, "A") = MyRecordset!Field1
      Cells(Row, "B") = MyRecordset!Field2
   End With
   
   ' Add new record and set field values
   MyRecordset.AddNew
   MyRecordset!Field1 = "Some data"
   MyRecordset.Update
   
   ' Delete current record
   MyRecordset.Delete

   ' Create headers and move data
   With Sheets("Sheet1")
      For Column = 0 To MyRecordset.Fields.Count - 1
         .Cells(1, Column + 1).Value = MyRecordset.Fields(Column).Name
      Next
      .Range(.Cells(1, 1), .Cells(1, MyRecordset.Fields.Count)).Font.Bold = True
     .Cells(2, 1).CopyFromRecordset MyRecordset
   End With

   ' Close recordset
   MyRecordset.Close

   ' Close database
   MyDatabase.Close

Kevin
 
01.13.2006 at 12:17PM PST, ID: 15695230

Rank: Guru

Patrick,

"But are the SQL Select statements in VBA different from Access SQL?"

In Access the tablename is always added to the fieldname like:

"SELECT Orders.OrdersDate FROM Orders;"

In VBA we can choose to use the same structure but usually (at least I do) skip the tablename:

"SELECT OrdersDate FROM Orders;"

In Access the INNER JOIN expression is likely to be used then the WHERE & AND expressions. In VBA we can choose to use one of them.

Access support SQL 92  which is also supported in VBA.

Since we can achieve the same output with different approaches (both supported in SQL 92, SQL 2003) the answer to Your questions is no in view of the above.

If You're comfortable with how Access create SQL expressions then I suggest that You set up the SQL expressions in Access and then create strings in VBA that use them. In that way You always assure that the expressions work as expected (although in my personal opinion it create some unnecessary overhead).

Hopefully the above shed some more light over it.

Kind regards,
Dennis
 
01.13.2006 at 12:38PM PST, ID: 15695406
Dennis:

Thanks for your advice.
The problem is my SELECT statement contains the variables from  comboboxes or listboxes. The reason I do this is most of these users do not have Access on their computers, so I try to store the data in Access and transfer to Excel whenever we update it.  

Patrick
 
01.13.2006 at 01:58PM PST, ID: 15696076

Rank: Master

Your select statement should be in a string variable

strSQL = "SELECT " & variable & " FROM tblTest1 Where....... "

etc.
 
01.13.2006 at 03:10PM PST, ID: 15696633
Testing=Tablename
Value=FieldName
cboanalystName=combobox variable

Here is my statement:
sqlStr = "SELECT Testing.[Value] " & _
    "From Testing " & _
    "Where Testing.[AnalystName]= '" & Me.cboAnalystName.Value & "';"

I want to add more criteria variables. ie. more combobox2, combobox3,  and so on.... so that there will be only one "Value" returned in a textbox.
How to I do that? Adding a couple AND in the Where Statement, Can anyone just give me a sample?
 
and then, how do I capture it into a textbox?

Thanks Everyone


 
 
01.13.2006 at 03:18PM PST, ID: 15696723

Rank: Sage

yuen338:

You can add the other variables within the sqlStr string just as you stated above.  Once you have it return the value just set the textbox.value to the returned value.

Jaes
 
01.13.2006 at 03:24PM PST, ID: 15696784
Please modifiy my SELECT STATMENT if you don't mind. and what is the code for setting the textbox.value to captur the returned value.

Patrick
 
01.13.2006 at 03:31PM PST, ID: 15696865

Rank: Sage

Patrick:

Well, personally I have only run SQL straight into a spreadsheet because my query result returned more than one value.  The way I do it is create the SQL in Access and then copy and paste it into Excel putting quotes around it and then anytime I am selecting items in my HAVING statement of the SQL statement I insert variables surrounded by    '" & Variable & "'    If it returns only one value I would assume you could just assign the result to a the textbox value in Excel.

Jaes
 
01.17.2006 at 06:15AM PST, ID: 15719347

Rank: Master

Try This,

sqlStr = "SELECT Testing.[Value] " & _
    "From Testing " & _
    "Where Testing.[AnalystName]= '" & Me.cboAnalystName.Value & ", " & Me.SecondControlVariable & ", " & Me.etcetcetc. & ";"

That will add more selection criteria.  If you want more than just the analyst name you could have:

sqlStr = "SELECT Testing.[Value] " & _
    "From Testing " & _
    "Where Testing.[AnalystName]= '" & Me.cboAnalystName.Value & " AND Testing.[Whatever Else] = " & Me.SecondControlVariable & ";"
Accepted Solution
 
 
20080236-EE-VQP-29