Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Help rephase

Status
Not open for further replies.

Hungoverhippie

Programmer
Feb 7, 2002
11
0
0
US
I need to make a literal out of a string.

Example:

*'Textfile Data reads as follows
rst1.fields("test1")
rst1.fields("test2")



dim ary(2)
open "c:\mydir\testfile.txt" for input as #1
for x=1 to 2
line Input #1, ary(x)
Next x
close #1
'all my ado connection stuff here then

a$=ary(1)
b$=ary(2)

What I get back is

a$="rst1.fields("test1")"
b$="rst1.fields("test2")"

what I want is the liternal of ary(1) and ary(2) as in whats in the recordset fields for test1 and test2.

Maybe this makes a little more since then before..

Thanks for anyones help in advance..
HungOverHippie
















 
Well, your code is doing what you're telling it to do. In other words you told it to read in the first line and put it into the first position of your array and so on. It's doing that.

However it looks to me like what you want to do is read values from a recordset. You don't need to read from a file for this. Something like the following is what you need. (Of course I might not understand at all what you're trying to do)

Dim db As Database
Dim rs as Recordset

Set db = CurrentDb()
Set rs = db.OpenRecordset("MyTable")
With rs
.MoveFirst
Do Until .EOF
MyVariable = .MyField
'do something with MyVariable here
.MoveNext
Loop
End With

I just banged this in with out the benefit of VBA so I might not have the sintax perfect, but this is the general idea of working with a recordset.
"The Key, The Whole Key, and Nothing But The Key, So Help Me Codd!"
 
No need to read from a text file for this project. put differently I need to how to make a$=ary(10) equal the liternal statement in that array not the string of it..


Thanks
 
I'm afraid I may not understand what you are trying to do.

What you are doing is putting a string into an array. The only thing you'll get back out is that same string. You say you don't want the string, but that's what you put in.

It still sounds to me like you want to read from a recordset. If you are trying to process 'rst1.fields("test1")' as code perhaps you should look at the Eval() function, although I'm not sure it will help you here.
"The Key, The Whole Key, and Nothing But The Key, So Help Me Codd!"
 
where another example of the same thing i am trying to get working...
This fails reading the select statement from file but works if hardcoded


Dim cnn2 As ADODB.Connection
Dim rst As ADODB.Recordset
Dim strng As String


Set cnn2 = New ADODB.Connection
cnn2.ConnectionString = "DSN=test;UID=test;PWD=test;"
cnn2.Open

Set rst = New ADODB.Recordset
Open "c:\Out\test.txt" For Input As #1
Input #1, strng
'saved in the txt file as "SELECT * FROM Orders where dte>='" & lbDAY1 & "'"
'lbDAY1 is a label on the form
rst.Open strng, cnn2, adOpenStatic, adLockReadOnly, adCmdText
If rst.RecordCount > 1 Then

Maybe this will make some since of what i am trying to accomplish


Thanks,
Hungoverhippie
 
Don't know if I really understand what you need, but if you are running your select against an access database, then the dte (field name) must be [dte].
And it may or may not require a semicolon at the end (;).
Good Luck.
 
I'm at my home machine here and I don't have VB loaded so I can't play with your code. But I'm pretty sure I understand now what you're trying to do. Why you want to put your string into a file I don't know, but I'll skip that aspect.

Your problem lies in your concatenation of lbDAY1 with the rest of the string. Since you store lbDAY1 as part of the string it is being evaluated as a string. It never looks for the label. In other words you are returning a recordset where "dte >= lbDAY1"! Clearly impossible.

You must do the concatenation in code before you apply the 'strng' argument to the .Open method of rst. This is where lbDAY1 is available as a variable. You could still store the rest of the string in a file, but not your lbDAY1 variable.

So, store this part: "SELECT * FROM Orders where dte>='" in your file and do something like this:

Set rst = New ADODB.Recordset
Open "c:\Out\test.txt" For Input As #1
Input #1, strng
'strng is "SELECT * FROM Orders where dte>='" in the file
strng = strng & lbDAY1 & "'"
'NOW you have a string that includes the value of lbDAY1, not the string 'lbDAY1'
rst.Open strng, cnn2, adOpenStatic, adLockReadOnly, adCmdText
If rst.RecordCount > 1 Then


Now, one other thing: if dte is a date value you can't surround your variable with quotes; in VB it has to be surrounded with pound (#) signs. Just a bit more Microsoft confusion for you.

"The Key, The Whole Key, and Nothing But The Key, So Help Me Codd!"
 
You might want to have a look at the Microsoft Script Control, which allows limited VBScript interpretation at run time. This has both Execute and Eval methods which allow you to interpret a VBScript string and return a value from it. You need to bind any objects in your VB code into the control via the AddObject method before you call these methods.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top