Results 1 to 12 of 12

Thread: evaluating values in variables

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2002
    Location
    NC
    Posts
    11

    evaluating values in variables

    I am working on a table driven app. I have placed all of my Select and insert statements in an sql 7 DB table. Things seemed to be going great until I needed to do an insert using an insert statement stored in the table. I query the DB to get the insert statement, place the statement in a variable. The problem is that the insert statement refers to the .field(x) property of a recordset and when the insert statement is stored in the variable it stores ".Recordset.Field(x)" instead of the value of .recordset.field(x). How do I evaluate the insert statement or the contents of the variable the insert statement is stored in to reflect the value of the .field(x) property?

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: evaluating values in variables

    Originally posted by jmichael2468
    I am working on a table driven app. I have placed all of my Select and insert statements in an sql 7 DB table. Things seemed to be going great until I needed to do an insert using an insert statement stored in the table. I query the DB to get the insert statement, place the statement in a variable. The problem is that the insert statement refers to the .field(x) property of a recordset and when the insert statement is stored in the variable it stores ".Recordset.Field(x)" instead of the value of .recordset.field(x). How do I evaluate the insert statement or the contents of the variable the insert statement is stored in to reflect the value of the .field(x) property?
    your asking for a world of trouble doing it this way.. you would be better off storing the sql statements in a text file than you would do a DB.. because you are going to get very frustrated with all your ' and " that you are going to have to use to get this to work right

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Better still, go with Stored Procedures.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535
    So whatz the problem in that? Just build your query by putting together the string from database and the values from your variables and fire it to the SQL engine!

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    The problem seems to be that he's getting


    INSERT INTO myTable
    (Field1, Field2, Field3)
    VALUES
    (.Recordset.Field(x), .Recordset.Field(x), .Recordset.Field(x))

    Instead of

    INSERT INTO myTable
    (Field1, Field2, Field3)
    VALUES
    ('1', '2', '3')
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Lively Member RobIII's Avatar
    Join Date
    Jul 2002
    Location
    Netherlands
    Posts
    112
    It think your query is wrong....

    It think this is what you have:

    VB Code:
    1. Connection.Execute "Insert into MyTable (FieldA, FieldB, FieldC) Values (.RecordSet.Field(0),.RecordSet.Field(1),.RecordSet.Field(2))"

    But I think it should be:

    VB Code:
    1. Connection.Execute "Insert into MyTable (FieldA, FieldB, FieldC) Values (" & .RecordSet.Field(0) & "," & .RecordSet.Field(1) & "," & .RecordSet.Field(2) & ")"


    Or, if you're inserting strings:

    VB Code:
    1. Connection.Execute "Insert into MyTable (FieldA, FieldB, FieldC) Values ('" & .RecordSet.Field(0) & "','" & .RecordSet.Field(1) & "','" & .RecordSet.Field(2) & "')"

    Make sure though that the .recordset.field(0) etc. don't contain a ' character. That'll "crash" the query.
    If it does, use this (alway safe) solution:

    VB Code:
    1. Sub FixSQL(strVal as string)
    2.   FixSQL = Replace(strVal,"'","''")
    3. End Sub
    4.  
    5. Connection.Execute "Insert into MyTable (FieldA, FieldB, FieldC) Values ('" & FixSQL(.RecordSet.Field(0)) & "','" & FixSQL(.RecordSet.Field(1)) & "','" & FixSQL(.RecordSet.Field(2)) & "')"

    TaDaaaaa.... This should help....
    Rob.

    Window XP: From the makers of DoubleSpace...

    Press ANY key... NO, NO, NO!!! NOT THAT ONE!!!!

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2002
    Location
    NC
    Posts
    11
    Techgnome is correct! I was getting


    INSERT INTO myTable
    (Field1, Field2, Field3)
    VALUES
    (.Recordset.Field(x), .Recordset.Field(x), .Recordset.Field(x))

    Instead of

    INSERT INTO myTable
    (Field1, Field2, Field3)
    VALUES
    ('1', '2', '3')

    and the reason I was getting this is because the sql statement coming from the table wasn't being evaluated with the field values before being stored in the variable. I did find a fix for the problem though. If anyone is interested here is the code.

    it is ugly but it does work.


    Dim x1 As Integer, x2 As Integer
    Dim MidStr As String
    Dim OffSet As Integer
    OffSet = 19


    Do While x2 < Len(sqlStatement)
    x1 = InStr(sqlStatement, ".sRecordset.Fields(")
    x2 = InStr(x1, sqlStatement, ")")
    MidStr = Mid$(sqlStatement, x1 + OffSet, (x2 - (x1 + OffSet)))
    sqlStatement = Replace(sqlStatement, Mid$(sqlStatement, x1, x2 - x1 + 1), .sRecordset.Fields((CInt(MidStr))))
    Loop

    Thanks guys

  8. #8
    Lively Member RobIII's Avatar
    Join Date
    Jul 2002
    Location
    Netherlands
    Posts
    112
    Did you even try the option I gave ya? That should work. And it's not as AWFUL as your current solution.

    Want another AWFUL solution? Try this:

    VB Code:
    1. Dim strSQL as string
    2.  
    3. strSQL = "INSERT INTO myTable (Field1, Field2, Field3) VALUES .Recordset.Field(1), .Recordset.Field(2), .Recordset.Field(3))"
    4.  
    5. strSQL = Replace(strSQL, ".Recordset.Field(1)", 1001)
    6. strSQL = Replace(strSQL, ".Recordset.Field(2)", 2001)
    7. strSQL = Replace(strSQL, ".Recordset.Field(3)", 3001)
    8. oConn.Execute strSQL

    That will result in putting values 1001, 2001 and 3001 in your fields. But as mentioned: AWFUL SOLUTION.

    Just use the first suggestion I gave you before....

    <grmbl grmbl>
    Last edited by RobIII; Sep 24th, 2002 at 08:37 AM.
    Rob.

    Window XP: From the makers of DoubleSpace...

    Press ANY key... NO, NO, NO!!! NOT THAT ONE!!!!

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2002
    Location
    NC
    Posts
    11
    The problem was not in the query. The problem was that the queries were constructed and stored in a SQL table. When I pulled these queries into a variable using VB the values in the .field properties were not evaluated by VB.

    Example:

    I stored the following in a sql table called Qs in a column called Colx.

    "INSERT INTO myTable
    (Field1, Field2, Field3)
    VALUES
    ('" & .Recordset.Field(1) & "','" & .Recordset.Field (2) & "','" & .Recordset.Field(3) & "')"

    After I open the the recordset I put the query in a variable like so

    sqlstatement = .recordset.field(colx)

    once that is done the value in
    sqlstatement is

    "INSERT INTO myTable
    (Field1, Field2, Field3)
    VALUES
    ('" & .Recordset.Field(1) & "','" & .Recordset.Field (2) & "','" & .Recordset.Field(3) & "')"

    when it should be

    "INSERT INTO myTable
    (Field1, Field2, Field3)
    VALUES
    ('1','2','.3')"

    Give it a try and you'll see the problem I was having. It wasn't a problem with the query or the quotes in the query. It was a dynamic evaluation problem that has now been resolved. Thank you for taking the time to reply.

    Michael

  10. #10
    Lively Member RobIII's Avatar
    Join Date
    Jul 2002
    Location
    Netherlands
    Posts
    112
    DID YOU EVEN TRY MY SOLUTION? Sjees....

    Just try it. What you were doing was just storing the "text". You don't HAVE to evaluate the values.

    What you are doing is this

    VB Code:
    1. Dim A as integer
    2. Dim strQuery as text
    3.  
    4. A = 12
    5. strQuery = "Select * from MyTable where MyID = A"

    This wil result in the query: "Select * from MyTable where MyID = A". Now, I'm sughesting this:

    VB Code:
    1. Dim A as integer
    2. Dim strQuery as text
    3.  
    4. A = 12
    5. strQuery = "Select * from MyTable where MyID = " & A

    Wich will result in "Select * from MyTable where MyID = 12".

    That's all... Just give it a go. It will work!!!

    Sjeess... Where's my valium?
    Rob.

    Window XP: From the makers of DoubleSpace...

    Press ANY key... NO, NO, NO!!! NOT THAT ONE!!!!

  11. #11

    Thread Starter
    New Member
    Join Date
    Jun 2002
    Location
    NC
    Posts
    11
    Number one, this is not what I was doing.

    your code:

    visual basic code:-------------------------------------------------------------
    Dim A as integer
    Dim strQuery as text

    A = 12
    strQuery = "Select * from MyTable where MyID = A"
    ------------------------------------------------------------------------------------



    But to give you an understanding of what I am doing look at this.

    Again your code:

    visual basic code:-------------------------------------------------------------
    Dim A as integer
    Dim strQuery as text

    A = 12
    strQuery = "Select * from MyTable where MyID = " & A
    --------------------------------------------------------------------------------


    If you take the query "Select * from MyTable where MyID = " & A and place that in a field of a table you have the same thing. Now, store the value from that field in a string variable like so


    visual basic code:-------------------------------------------------------------
    dim A as integer
    dim StrQuery as string

    A = 12

    StrQuery = .recordset.field(1) 'the field holding the query
    ------------------------------------------------------------------------------------

    now the variable StrQuery has the following string:

    ""Select *from MyTable Where MyID = " & A "

    A is not evaluated to 12 because VB does not see A as a variable, but as part of the above string.

    That is why your suggestion will not work! I know it won't work because that was my very first attempt and the outcome was what I have been stating from the start.

  12. #12
    Lively Member RobIII's Avatar
    Join Date
    Jul 2002
    Location
    Netherlands
    Posts
    112
    Why didn't you say so?

    But still, the loop sucks. Try using the Replace function (look it up in the helpfiles. You might want to consider the vbTextCompare option to make it case-insensitive)...
    Rob.

    Window XP: From the makers of DoubleSpace...

    Press ANY key... NO, NO, NO!!! NOT THAT ONE!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width