473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to connect ADO dynamically

22
Hello experts..

I'm using ADO,VB6 and MS Access as database. I would like to create a form as template for many subjects. I need to make dynamic connection based on subject. I created a module for ADO connection then used IF statement but fail. Here is my code in the module(.bas):
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Public snconn As ADODB.Connection
  3. Public sncmd As ADODB.Command
  4. Public rs As ADODB.Recordset
  5.  
  6. Public Sub connectdb()
  7. Set snconn = New ADODB.Connection
  8.     snconn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
  9.                 & "Data Source=" _
  10.                 & GetAppPath _
  11.                 & "KSSR.mdb"
  12.     Set sncmd = New ADODB.Command
  13.     Set sncmd.ActiveConnection = snconn
  14.     sncmd.CommandType = adCmdText
  15.  
  16. End Sub
  17.  
  18. Public Sub DisconnectDB()
  19.  
  20.     snconn.Close
  21.     Set sncmd = Nothing
  22.     Set snconn = Nothing
  23.  
  24. End Sub
  25.  
  26. Public Function GetAppPath() As String
  27. '-----------------------------------------------------------------------------
  28.  
  29.     Dim strAppPath As String
  30.  
  31.     strAppPath = App.Path
  32.  
  33.     If Right$(strAppPath, 1) <> "\" Then
  34.         strAppPath = strAppPath & "\"
  35.     End If
  36.  
  37.     GetAppPath = strAppPath
  38.  
  39. End Function
  40.  
  41. Public Sub sqlSubject()
  42. If frmPBS.tabSn2.Visible = True Then
  43. Set rs = New ADODB.Recordset
  44. With rs
  45.  
  46.     .Source = "Select * from sn2"
  47.     .ActiveConnection = snconn
  48.     .CursorLocation = adUseClient
  49.     .CursorType = adOpenStatic
  50.     .LockType = adLockOptimistic
  51.     .Open
  52.     .Sort = "NAMA ASC"
  53. End With
  54. End If
  55. If frmPBS.tabMath2.Visible = True Then
  56. Set rs = New ADODB.Recordset
  57. With rs
  58.  
  59.     .Source = "Select * from math2"
  60.     .ActiveConnection = snconn
  61.     .CursorLocation = adUseClient
  62.     .CursorType = adOpenStatic
  63.     .LockType = adLockOptimistic
  64.     .Open
  65.     .Sort = "NAMA ASC"
  66. End With
  67. End If
  68. End Sub
Here is my code in form_load:
Expand|Select|Wrap|Line Numbers
  1. connectdb
  2. sqlSubject
  3. If rs.RecordCount > 1 Then
  4. rs.MoveNext
  5. end if
Hope someone can help. The error says "Object variable or With block variable not set".
Feb 1 '13 #1
3 1667
Rabbit
12,516 Expert Mod 8TB
It would help to know which line is causing the error.
Feb 1 '13 #2
mdtam
22
the yellow line shows on "rs.recordcount>1".
Feb 1 '13 #3
Rabbit
12,516 Expert Mod 8TB
That means that your rs varible never got set. Which most likely means that frmPBS.tabSn2.Visible and If frmPBS.tabMath2.Visible are both false.
Feb 1 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Tom Haddon | last post by:
Hi Folks, Newbie question here. I'm trying to set up some unit testing for a database abstraction class, and the first thing I want to test is the connection parameters. So, my question is, how do...
1
by: Domer | last post by:
In my program, I load dynamically a DLL and I'm able to get a function pointer of type "int (__cdecl *)(void)" of any function in the DLL. If a DLL has a static class function, which calls the...
3
by: Sander Smeenk | last post by:
Hello! I'm trying to set up replication between two servers and even though I did everything according to the documentation, the slave keeps failing to connect to the master like this: |...
3
by: T-Man | last post by:
Hello, I have an application in C# that makes calls to a web service and the app currently contains a reference to a WSDL. Since the server technology is a commercial product, some customers...
2
by: macca | last post by:
Hi, I am writing a GUI application. It will have a number of user defined controls( I plan to use/create a user defined control that will output alarm states that the user can also select and...
1
by: Ed S | last post by:
Hi All, We're upgrading our web project - currently we have a web app and a web service - both written in C#. The web app communicates with the web service for login authentication, the list of...
3
by: JL | last post by:
I have a VB.NET desktop program that reads/writes data to a server using a Java-based Web Service. This web service, in identical formats, is located on several servers with each server being a...
4
by: Kevin Blount | last post by:
bit long winded this one, so stick with me: I'm trying to create a form that can go to one of 3 places, depending on various elements. My form control looks like this: <form runat="server"...
0
by: SecretSquirrel | last post by:
Hi, I have created a small local web app with ASP.NET. I run Microsoft Application Center Test and create a new test project. Then, I create a new test dynamically. MS ACT pops up a new...
0
by: Martin Caba | last post by:
hi i`m using visual basic express 2010 and SQL server 2008 R2 this is my problem i create database in sql, then i connect to database by visual basic. like <?xml version="1.0" encoding="utf-8"?>...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.