Solved

setting a variable to Data field contents

Posted on 1998-07-15
3
198 Views
Last Modified: 2008-03-06
I have declared my Variables as string and need to set each variable to the contents of a database field.  I set the variables like this:

Dim coststat1 As String
Dim coststat2 As String
Dim coststat3 As String
Dim coststat4 As String
Dim Schedstat1 As String
Dim Schedstat2 As String
Dim Schedstat3 As String
Dim Schedstat4 As String
Dim Perfstat1 As String
Dim Perfstat2 As String
Dim Perfstat3 As String
Dim Perfstat4 As String
Dim antCoststat As String
Dim antSchedstat As String
Dim antPerfstat As String

Then I try to set the variable to the current content of a database field with the following:
coststat1 = frmProgram.Data1.Recordset.Fields("coststat_1")
    coststat2 = frmProgram.Data1.Recordset.Fields("coststat_2")
    coststat3 = frmProgram.Data1.Recordset.Fields("coststat_3")
    coststat4 = frmProgram.Data1.Recordset.Fields("coststat_4")
    Schedstat1 = frmProgram.Data1.Recordset.Fields("schedstat_1")
    Schedstat2 = frmProgram.Data1.Recordset.Fields("schedstat_2")
    Schedstat3 = frmProgram.Data1.Recordset.Fields("schedstat_3")
    Schedstat4 = frmProgram.Data1.Recordset.Fields("schedstat_4")
    Perfstat1 = frmProgram.Data1.Recordset.Fields("perfstat_1")
    Perfstat2 = frmProgram.Data1.Recordset.Fields("perfstat_2")
    Perfstat3 = frmProgram.Data1.Recordset.Fields("perfstat_3")
    Perfstat4 = frmprogram.data1.Recordset.Fields("perfstat_4")


I get an error "Object variable or With Block Varible not set"

What am I doing wrong?????
0
Comment
Question by:clarwc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Accepted Solution

by:
biko071598 earned 50 total points
ID: 1465828
The error "Object variable or With Block Varible not set" should read "I'm trying to access an object that doesn't exist!"

It looks like you are trying to get cheap data access by using a data control instead of opening a database object and opening a recordset object.  If this is the case, your data control's recordset object is not initialized. Possible reasons are: no/incorrect database specified, no/incorrect recordsource property specified. Set these properties in the form load event and make sure the form loads before you access the data.  

You can cut down on the typing by using an alternate syntax.

Use:
"coststat2 = frmProgram.Data1.Recordset!coststat_2"

Instead of:
"coststat2 = frmProgram.Data1.Recordset.Fields("coststat_2")
"

Good luck
0
 

Author Comment

by:clarwc
ID: 1465829
bike,

I tried your suggestion and got the same message.  I don't care so much about saving code as I do having the function work correctly.  I have the statements in my Form_load event.  do I need to open the database first?  

I tried :

Dim mydb as database
Dim myrs as recordset

Set mydb = workspaces(1).opendatabase("c:\205gui\205 data.mdb")
Set myrs = mydb.openrecordset("programs", dbopentable)


But it doesn't work....

Any other ideas?????
0
 

Expert Comment

by:biko071598
ID: 1465830
>>Set mydb = workspaces(1).opendatabase("c:\205gui\205 data.mdb")
Could it be that the filename is inccorrect (i.e. embedded space?)

try this...

Dim mydb as database
Dim myrs as recordset

Set mydb = opendatabase("c:\205gui\somedata.mdb") <--"Make sure path if fine..use 8.3 file notation like in msdos...)
 
Set myrs = mydb.openrecordset("programs", dbopendynaset) <--- use a dynaset of you can
 
set data1.recordset = myrs

mind that you must have the professional edition of vb4 or vb5 to do this.  What version are you using?

You can also try this instead...
If you simply want to verify the datacontrol, pull up the form and set the database and recordsource properties.  Then put at least one dataaware control on the form and point it to the datacontrol.  Test the form by showing it verifying that you have data.  Then try the routine....

Good Luck



0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question