Click here to Skip to main content
16,015,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone tell me what us wrong with this code? The error "Path not found" was thrown.
uSQL = "SELECT soa_month FROM DMF_SocsoArrears WHERE SOA_PAYMONTH = " & Month(dtpSocsoDate.Value) & " AND " & _
       "SOA_PAYYEAR = " & Year(dtpSocsoDate.Value) & " AND (SOA_SOAMTEE+SOA_SOAMTER) > 0 "

   RS1.Open vSQL, Conn, adOpenStatic, adLockReadOnly
   RS2.Open uSQL, Conn, adOpenStatic, adLockReadOnly
   'If RS2.RecordCount > 0 Then
   'While Not RS2.EOF
    If uSQL = "1" Then
       tFileName = "January.TXT"
    ElseIf uSQL = "2" Then
       tFileName = "February.TXT"
    ElseIf uSQL = "3" Then
       tFileName = "March.TXT"
    ElseIf uSQL = "4" Then
       tFileName = "April.TXT"
    ElseIf uSQL = "5" Then
       tFileName = "May.TXT"
    ElseIf uSQL = "6" Then
       tFileName = "June.TXT"
    ElseIf uSQL = "7" Then
       tFileName = "July.TXT"
    ElseIf uSQL = "8" Then
       tFileName = "August.TXT"
    ElseIf uSQL = "9" Then
       tFileName = "September.TXT"
    ElseIf uSQL = "10" Then
       tFileName = "October.TXT"
    ElseIf uSQL = "11" Then
       tFileName = "November.TXT"
    ElseIf uSQL = "12" Then
       tFileName = "December.TXT"
    End If
   'Wend
   'End If
   'tFileName = "January.TXT"
   intFileHandle2 = FreeFile

   Dim sPath As String
   sPath = txtSocsoLoc.Text & tFileName
   MsgBox "" & tFileName, vbInformation, Me.Caption
   'If Dir(spath) <> "" Then spath = txtSocsoLoc.Text & "NOMANTH.TXT"
   Open sPath For Output As #intFileHandle2


What I have tried:

So I tried to see the outcome of the tFileName via a msg box it returns nothing.
Posted
Updated 23-May-18 21:57pm

You are checking uSQL which is the SQL query string. You have to check the recordset field values instead (untested):
VB
If RS2.RecordCount > 0 Then
    While Not RS2.EOF
        If RS2!soa_month = "1"
            ' January
        EndIf
        RS2.MoveNext
    Wend
End If
Instead of the short (bang) syntax form RS2!soa_month you can also use RS2.Fields("soa_month").Value, RS2("soa_month").Value, or RS2.Fields(0).Value (here the zero is the zero based index of the returned recordset fields which must be zero here because you are querying only a single field).
 
Share this answer
 
Comments
Member 12609377 24-May-18 3:47am    
THANKS A LOTTTTTTTTTT!!!!!!!!!!!!!!!!!!!!!! YOU SAVED MY LIFE! I'm really new to vb6 and sql and still learning, sorry if I asked something silly, I guess I did.
Some weird things in your code:
- uSQL contains the text of your SQL query, not the answer.
- one can expect the result to be in RS2
- one can expect a month number to be numeric, and not a string
To know what is what, use the debugger and inspect variables
-----
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Debugging in Excel VBA - EASY Excel Macros[^]
MS Excel 2013: VBA Debugging Introduction[^]
How to debug Excel VBA - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
AS CPallini pointed out you are comparing uSql to other string values that are not even close to what uSql is.
For example, if tpSocsoDate is 01/01/2018 the uSql will be

VB
"SELECT soa_month FROM DMF_SocsoArrears WHERE SOA_PAYMONTH = 1 AND SOA_PAYYEAR = 2018 AND (SOA_SOAMTEE+SOA_SOAMTER) > 0 "


This is never equal to the conditions in your if else if ladder.

What you want to do is execute the command string in uSql on the database and store the value returned inside a local variable. Then compare that variable in the if else if ladder not the sql command.

On another note, never concatenate strings to make an SQL command. It makes your code vulnerable to sql injection attacks. Use parametrized queries instead.
 
Share this answer
 
v2
Well, the answer is simple: the uSQL string doesn't match any of the expected conditions. That's not a surprise, since it is just the SQL QUERY text.
 
Share this answer
 
Comments
Member 12609377 24-May-18 3:38am    
Thanks for replying. But the problem is, even when i didn't put any condition ( uSQL = "SELECT soa_month FROM DMF_SocsoArrears ) the output is still the same. For the conditions, (1-12) there is a line in the database where the soa_month = 1 and its an int column. please help thanks.
GKP1992 24-May-18 3:38am    
Too subtle a hint I think. :D
Maciej Los 24-May-18 4:17am    
5ed!
So here is what i changed and the problem was solved. Thanks everyone!


<pre> vSQL = vSQL & " GROUP BY SOA_COMPNO, SOA_FAC, EEP_NAME, EEP_IC, EEP_SOCSONO, dtjoin, dtresign ORDER BY SOA_COMPNO, SOA_FAC, EEP_NAME "
    
    uSQL = "SELECT soa_month FROM DMF_SocsoArrears WHERE SOA_PAYMONTH = " & Month(dtpSocsoDate.Value) & " AND " & _
        "SOA_PAYYEAR = " & Year(dtpSocsoDate.Value) & " AND (SOA_SOAMTEE+SOA_SOAMTER) > 0 "
    
    RS1.Open vSQL, Conn, adOpenStatic, adLockReadOnly
    RS2.Open uSQL, Conn, adOpenStatic, adLockReadOnly
    If RS2.RecordCount > 0 Then
     While Not RS2.EOF
        If RS2!soa_month = "1" Then
           tFileName = "January.TXT"
        ElseIf RS2!soa_month = "2" Then
           tFileName = "February.TXT"
        ElseIf RS2!soa_month = "3" Then
           tFileName = "March.TXT"
        ElseIf RS2!soa_month = "4" Then
           tFileName = "April.TXT"
        ElseIf RS2!soa_month = "5" Then
           tFileName = "May.TXT"
        ElseIf RS2!soa_month = "6" Then
           tFileName = "June.TXT"
        ElseIf RS2!soa_month = "7" Then
           tFileName = "July.TXT"
        ElseIf RS2!soa_month = "8" Then
           tFileName = "August.TXT"
        ElseIf RS2!soa_month = "9" Then
           tFileName = "September.TXT"
        ElseIf RS2!soa_month = "10" Then
           tFileName = "October.TXT"
        ElseIf RS2!soa_month = "11" Then
           tFileName = "November.TXT"
        ElseIf RS2!soa_month = "12" Then
           tFileName = "December.TXT"
        End If
     RS2.MoveNext
     Wend
    End If
    'tFileName = "January.TXT"
    intFileHandle2 = FreeFile
    
    Dim sPath As String
    sPath = txtSocsoLoc.Text & tFileName
    MsgBox "" & tFileName, vbInformation, Me.Caption
    'If Dir(spath) <> "" Then spath = txtSocsoLoc.Text & "NOMANTH.TXT"
    Open sPath For Output As #intFileHandle2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900