Visual Basic

Moderators: None (Apply to moderate this forum)
Number of threads: 18011
Number of posts: 55384

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Easier Way To Store SQL Results? Posted by Cory on 24 Feb 2005 at 1:48 PM
I Am Using SQL Alot In A Project, But For Some Reason I Feel I'm Doing Way More Work Then I Have To. Curently, I Am Storing Variables By Checking Each Field, Then Storing Accordingly.

Do Until rs.EOF
    EmpSum.CurRec = EmpSum.CurRec + 1
    CmdEdit(EmpSum.CurRec).Visible = True
    For Each fld In rs.Fields
        If fld.Name = "11a" Then
            EmpSum.SumHours(1, 1, 1) = fld.Value
        ElseIf fld.Name = "12a" Then
            EmpSum.SumHours(1, 2, 1) = fld.Value
        ElseIf fld.Name = "13a" Then
            EmpSum.SumHours(1, 3, 1) = fld.Value
        ElseIf fld.Name = "14a" Then
            EmpSum.SumHours(1, 4, 1) = fld.Value
        End If
    Next
Loop


There Must Be An Easier Way To Do This Then To Check The Field Name For Every Record.

Any Ideas Would Be Appreciated.
Report
Re: Easier Way To Store SQL Results? Posted by dokken2 on 25 Feb 2005 at 5:46 AM
: I Am Using SQL Alot In A Project, But For Some Reason I Feel I'm Doing Way More Work Then I Have To. Curently, I Am Storing Variables By Checking Each Field, Then Storing Accordingly.
:
:
: Do Until rs.EOF
:     EmpSum.CurRec = EmpSum.CurRec + 1
:     CmdEdit(EmpSum.CurRec).Visible = True
:     For Each fld In rs.Fields
:         If fld.Name = "11a" Then
:             EmpSum.SumHours(1, 1, 1) = fld.Value
:         ElseIf fld.Name = "12a" Then
:             EmpSum.SumHours(1, 2, 1) = fld.Value
:         ElseIf fld.Name = "13a" Then
:             EmpSum.SumHours(1, 3, 1) = fld.Value
:         ElseIf fld.Name = "14a" Then
:             EmpSum.SumHours(1, 4, 1) = fld.Value
:         End If
:     Next
: Loop
: 

:
: There Must Be An Easier Way To Do This Then To Check The Field Name For Every Record.
:
: Any Ideas Would Be Appreciated.
:


Have you considered referencing each field directly? ie:

 Do Until rs.EOF
     EmpSum.CurRec = EmpSum.CurRec + 1
     CmdEdit(EmpSum.CurRec).Visible = True

             EmpSum.SumHours(1, 1, 1) = rs!11a
             EmpSum.SumHours(1, 2, 1) = rs!12a
             EmpSum.SumHours(1, 3, 1) = rs!13a
             EmpSum.SumHours(1, 4, 1) = rs!14a
  rs.MoveNext
 Loop
 


Report
Re: Easier Way To Store SQL Results? Posted by Cory on 25 Feb 2005 at 7:39 AM
: : I Am Using SQL Alot In A Project, But For Some Reason I Feel I'm Doing Way More Work Then I Have To. Curently, I Am Storing Variables By Checking Each Field, Then Storing Accordingly.
: :
: :
: : Do Until rs.EOF
: :     EmpSum.CurRec = EmpSum.CurRec + 1
: :     CmdEdit(EmpSum.CurRec).Visible = True
: :     For Each fld In rs.Fields
: :         If fld.Name = "11a" Then
: :             EmpSum.SumHours(1, 1, 1) = fld.Value
: :         ElseIf fld.Name = "12a" Then
: :             EmpSum.SumHours(1, 2, 1) = fld.Value
: :         ElseIf fld.Name = "13a" Then
: :             EmpSum.SumHours(1, 3, 1) = fld.Value
: :         ElseIf fld.Name = "14a" Then
: :             EmpSum.SumHours(1, 4, 1) = fld.Value
: :         End If
: :     Next
: : Loop
: : 

: :
: : There Must Be An Easier Way To Do This Then To Check The Field Name For Every Record.
: :
: : Any Ideas Would Be Appreciated.
: :
:
:
: Have you considered referencing each field directly? ie:
:
:
:  Do Until rs.EOF
:      EmpSum.CurRec = EmpSum.CurRec + 1
:      CmdEdit(EmpSum.CurRec).Visible = True
: 
:              EmpSum.SumHours(1, 1, 1) = rs!11a
:              EmpSum.SumHours(1, 2, 1) = rs!12a
:              EmpSum.SumHours(1, 3, 1) = rs!13a
:              EmpSum.SumHours(1, 4, 1) = rs!14a
:   rs.MoveNext
:  Loop
:  

:
:

Yes I Have, I Just Didn't Know How To Go About It In VB. I've Done It In PHP, So That's Why I Was Wondering If I Could Do It In VB. Thanks Alot.
Report
Re: Easier Way To Store SQL Results? Posted by Cory on 25 Feb 2005 at 10:16 AM
Can My Field Not Start With A Number? If The Field Starts With A Number, I Get This
Compile Error:

Expected: End Of Statement


Is There Any Way To Get Around This?
Report
Re: Easier Way To Store SQL Results? Posted by dokken2 on 25 Feb 2005 at 10:50 AM
There's several ways to reference the field and each has tradeoffs.

rs!field - good performance but not for fieldnames with a space.
rs("field name") - slow but fine with any valid fieldname
rs(1), rs(2), etc. - best perf. and fieldname irrelevant but poor for code readability

In Access you can also use: rs![field name] but that may not apply


: Can My Field Not Start With A Number? If The Field Starts With A Number, I Get This
:
: Compile Error:
: 
: Expected: End Of Statement
: 

:
: Is There Any Way To Get Around This?
:

Report
Re: Easier Way To Store SQL Results? Posted by infidel on 25 Feb 2005 at 1:20 PM
: There's several ways to reference the field and each has tradeoffs.
:
: rs!field - good performance but not for fieldnames with a space.
: rs("field name") - slow but fine with any valid fieldname
: rs(1), rs(2), etc. - best perf. and fieldname irrelevant but poor for code readability
:
: In Access you can also use: rs![field name] but that may not apply
:
:
: : Can My Field Not Start With A Number? If The Field Starts With A Number, I Get This
: :
: : Compile Error:
: : 
: : Expected: End Of Statement
: : 

: :
: : Is There Any Way To Get Around This?

rs.Fields("FIELDNAME").Value & vbnullstring

This is the safest way to refer to recordset fields in my experience. The vbnullstring is to prevent "Invalid Use of NULL" errors when assigning a database value to a non-Variant variable.

I have found the ! shortcut also has other odd behavior in some cases.


infidel

$ select * from users where clue > 0
no rows returned





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.