if else语句什么都不返回(VB6) [英] If else statement returning nothing (VB6)

查看:204
本文介绍了if else语句什么都不返回(VB6)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能告诉我这段代码有什么问题吗?抛出错误未找到路径。

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





我试过的:



所以我试图通过msg框查看tFileName的结果,它什么都不返回。



What I have tried:

So I tried to see the outcome of the tFileName via a msg box it returns nothing.

推荐答案

您正在检查 uSQL 这是SQL查询字符串。您必须检查记录集字段值(未经测试):

You are checking uSQL which is the SQL query string. You have to check the recordset field values instead (untested):
If RS2.RecordCount > 0 Then
    While Not RS2.EOF
        If RS2!soa_month = "1"
            ' January
        EndIf
        RS2.MoveNext
    Wend
End If

而不是短(bang)语法形式 RS2!soa_month 您还可以使用 RS2.Fields(soa_month)。值 RS2(soa_month)。值,或 RS2.Fields(0).Value (这里的零是返回的零基索引记录集字段,这里必须为零,因为你只查询一个字段)。

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).


一些奇怪的东西代码中的ngs:

- uSQL包含SQL查询的文本,而不是答案。

- 可以预期结果是在RS2中

- 可以预期月份数字是数字,而不是字符串

要知道什么是什么,请使用调试器并检查变量

---- -

你的代码没有你想象的那样,你不明白为什么!



有一个几乎通用的解决方案:在调试器上逐步运行代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你应该做什么,它没有找到错误,它只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



在Excel VBA中进行调试 - 简易Excel宏 [ ^ ]

MS Excel 2013:VBA调试简介 [ ^ ]

如何调试Excel VBA - YouTube [ ^ ]



这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。
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.


AS CPallini指出你正在比较uSql其他字符串值甚至不接近uSql。

例如,如果tpSocsoDate是01/01/2018,那么uSql将是



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

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





这绝不等于if else if梯形图中的条件。



你想要做的是在数据库中的uSql中执行命令字符串并存储在局部变量中返回的值。然后在if else if梯形图中比较该变量而不是sql命令。另外请注意,永远不要连接字符串来生成SQL命令。它会使您的代码容易受到SQL注入攻击。使用参数化查询代替。



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.


这篇关于if else语句什么都不返回(VB6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆