如何通过访问97表进入LOOP [英] How do I get to LOOP through my access 97 table

查看:100
本文介绍了如何通过访问97表进入LOOP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击一个带有字母的按钮时,说M我无法遍历我的表格以获得在Listview中显示的结果。我正在使用VB6和Access 97



我尝试过:



从我以前的应用程序中搜索互联网和各种编码方式的无数个小时。这是我第一次使用连接字符串。该按钮的代码如下:

When I click a button with a letter on, say M I cannot loop through my table to get results to display in a Listview. I am using VB6 and Access 97

What I have tried:

Numerous hours searching the internet and various coding ways, from previous applications I have done. This is my first using a connection string. The code for the button is as follows:

Private Sub cmd_SelectByLetter_Click(Index As Integer)
    Dim Kga_RS As Recordset
    Dim sql, myLetter As String
    Dim i As Long 'use long if the database has a huge amount of records; other wise use integer
    ' clear the listview
    lstv_AllKgaMembers.ListItems.Clear
    Set Kga_RS = New Recordset
'    Kga_RS.LockType = adLockOptimistic
'    Kga_RS.CursorType = adOpenKeyset
    myLetter = Chr(CLng(Index) + 65)
    sql = "SELECT * FROM [tbl_KGA_MEMBER_DETAILS] WHERE [F_Name] LIKE '" & myLetter _
        & "*' ORDER BY [F_Name] ASC"
    Kga_RS.Open sql, dbCon, , adLockOptimistic
    i = 0
    With lstv_AllKgaMembers
        Do Until Kga_RS.EOF
            FullName = Kga_RS!F_Name & " " & Kga_RS!L_Name
            ' "i" will be used to point to the row number for the items being added
            ' to the listview increment the i each time it loops past it
            i = i + 1
            ' now write the records from the recordset to the listview fields
            .ListItems.Add , , Kga_RS!KgaNo 'control id
            ' you can short cut this, but it is important not to do so check for blank fields,
            ' otherwise you'll get errors when there is a field that is blank (for example, if there is no address)
            If Not FullName = "" Then .ListItems(i).ListSubItems.Add , , FullName Else .ListItems(i).ListSubItems.Add , , ""
            Kga_RS.MoveNext
        Loop
    End With
    Kga_RS.Close
    cmd_SelectAllMembers.Enabled = True
    cmd_ClearAll.Visible = True
End Sub

推荐答案

假设这仍然是问题在这里一些事情要尝试



Assuming this is still an issue here are a few things to try

sql = "SELECT * FROM [tbl_KGA_MEMBER_DETAILS] WHERE [F_Name] LIKE '" & myLetter _
        & "%' ORDER BY [F_Name] ASC"

注意使用字符串连接是一个巨大的SQL注入风险,但我不记得你是否可以使用VB6参数化查询(我还没有使用它)安装多年)。



如果您在Access中编写查询,您将使用*作为通配符,但使用OleDb我认为您需要使用类似sql的'%'作为通配符。您可以随时删除WHERE,直到找到问题为止。



如果不起作用,请尝试

Note using string concatenation is a huge sql injection risk, but I don't remember if you can use parameterised queries with VB6 (and I haven't had it installed for years).

If you were writing the query in Access you would use * for the wildcard but using OleDb I think you need to use the sql-like '%' for the wildcard. You could always removed the WHERE altogether until you track down the problem.

If that doesn't work try putting

Kga_RS.MoveFirst

循环之前



如果仍然无效,请再次查看连接字符串。尝试

before the loop

If that still doesn't work then look again at your connection string. Try

Set DBcON = New Connection
Dim strConn As String
    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbFile & ";"
    conn.Open (strConn)



最后,你还没有说App.Path在哪里 - 如果那是(例如)Program Files(x86)那么任何添加到数据库的数据可能位于用户AppData文件夹中而不是App.Path中(假设您使用的是Windows> = Vista版本) - 尝试将数据库移动到显式文件夹(如C:\ temp)例如)


Finally, you have not said where App.Path is - if that is (e.g.) Program Files (x86) then any data added to the database will probably be in the users AppData folder and not in the App.Path (Assuming you are using a version of Windows >= Vista) - try moving the database to an explicit folder (like C:\temp for example)


这篇关于如何通过访问97表进入LOOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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