SQL 过滤 WHERE 字段

SQL filtering a WHERE field

这是我的第一个 post,因为我已经设法通过阅读所有现有的讨论来解决任何问题或学习任何我遇到的困难。然而这个问题让我非常难过!

我工作的公司将个人职位保存在职位编号下,目前是六位数字 207076。基本上我想做的是获取这六位数字,在数据库中搜索该职位并提取我可能需要的所有详细信息(客户和职位信息)。应该很简单。获取文件名,然后将其作为在相关字段中搜索的字符串。

不是真的,因为数据库中的每个职位编号都以 2 或 3 个字符开头,代表估算员的姓名首字母。作业编号的长度也是从 1000 开始的。为了说明如何输入,请抓取下面的屏幕

sample of db

首先我尝试了一个子字符串,只是去掉了前两个字符:

strSQL = "SELECT [id] FROM quotation WHERE substring(reference ,3 ,len(reference)) = " & jobno & " AND version = " & ver

这只在某些时候有效,因为我们的一些估算员输入了三个字符的首字母

其次,我尝试使用正确的命令并仅与最后 6 位数字进行比较

strSQL = "SELECT [id] FROM quotation WHERE Right(reference, 6)  = " & jobno & " AND version = " & ver & " ORDER BY [createdDate]

再次仅在某些时候有效,就好像您在数据库中找到名称中少于 6 位数字的任何作业时它会崩溃。

我最后的想法是引用 Excel 库并使用 VAL 函数,但我似乎无法在 SQL 语句中使用它。我真的不知道如何解决这个问题。

Coreldraw 的 VBA 似乎对 SQL 语句非常古怪;我读过的很多东西似乎并不适用,这让学习变得非常沮丧。我相信我 references to be correct?

完整代码如下(密码和数据库信息被删掉)

欢迎大家批评指教, 谢谢

Private Function descriptions(ByVal x As Double, y As Double, w As Double, h        As Double, jobno As String, ver As String)
'On Error GoTo ex
ActiveDocument.ReferencePoint = cdrTopLeft
Dim objConn As New ADODB.Connection
Dim objRS As New ADODB.Recordset
Dim objCmd As New ADODB.Command



Dim strSQL As String, path As String
Dim bol As Boolean
Dim recordsCounter As Long, a As Long, b As Long
Dim fieldvalue As Variant
Dim QR As String
Dim dat As Variant
Dim FULLID As String
Dim i As Long, j As Long, k As Long


'CONNECT TO SERVER AND DB
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


With objConn
            .Provider = "SQLOLEDB.1;Password=xxxxxxxxxxxxxxxxxxx;Persist Security Info=True;User ID=xxxxxxxxxxxxxxxxxxx;Initial Catalog=xxxxxxxxxx;"
            .ConnectionString = "Data Source=xxxxxxxxxxxxxxxxxxx;"
    .Open
End With

If objConn.State <> 1 Then MsgBox "You were unable to connect to the database!", vbInformation

'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

strSQL = "SELECT [id] FROM quotation WHERE substring(reference ,3 ,len(reference)) = " & jobno & " AND version = " & ver

'strSQL = "SELECT [id] FROM quotation WHERE Right(reference, 6)  = " & jobno & " AND version = " & ver & " ORDER BY [createdDate]

'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

objRS.Open strSQL, objConn, adOpenKeyset, adLockOptimistic
QR = objRS.Fields(0).Value
FULLID = objRS.Fields(1).Value
MsgBox FULLID

'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
strSQL = "SELECT [itemId], [quantity], [description] FROM quotationitems WHERE quotationid = " & [QR] & " ORDER BY [itemId];"
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

objRS.Close
objRS.Open strSQL, objConn, adOpenKeyset, adLockOptimistic




'RETRIEVE DESCRIPTIONS
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


Dim DX As Double, DY As Double, DW As Double, DH As Double
Dim drx As Double, dry As Double, drw As Double, drh As Double
Dim DESCRIP As New ShapeRange
Dim s1 As Shape
Dim descriprange As Shape
Dim DLINE As String

Dim Desc As String, itm As String, quantity As String

DX = x + (w * 0.005)
DY = y + (h * 0.001)

Do While Not objRS.EOF

        If objRS.Fields(0).Value <> -1 Then itm = Chr(64 + objRS.Fields(0).Value)
        If objRS.Fields(1).Value <> -1 Then quantity = objRS.Fields(1)
        If objRS.Fields(2).Value <> -1 Then Desc = objRS.Fields(2)
        DLINE = "ITEM " & replace(itm, vbCr, "  ") & "   Qty:  " & quantity & "   " & Chr(13) & Desc



        Set s1 = ActiveLayer.FindShape("DESCRIPTIONENTRYPOINT")
        s1.Duplicate
        s1.TEXT.Story = DLINE
        s1.TEXT.Story = replace(s1.TEXT.Story, vbCr & vbCr, "")
        s1.TEXT.Story = replace(s1.TEXT.Story, "ITEM ", vbCr & vbCr & "ITEM   ")
        DESCRIP.Add s1
        DY = DY - (s1.SizeHeight * 1.1)
        s1.ObjectData("Name") = "DESCRIPTION-PARAGRAPH"


    recordsCounter = recordsCounter + 1
    objRS.MoveNext

  Loop
  objRS.Close

    Dim TRS As ShapeRange
    Set TRS = ActiveLayer.FindShapes("DESCRIPTIONENTRYPOINT")
    TRS.Delete

'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


'POSITION + SCALE DESCRIPTION TEXT TO SUIT TEMPLATE
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

DESCRIP.GetBoundingBox DX, DY, DW, DH, False
DESCRIP.CreateSelection
Set DESCRIP = ActiveSelectionRange.ReverseRange

Set descriprange = ActiveLayer.FindShape("DESCRANGE")
descriprange.GetBoundingBox drx, dry, drw, drh, False


 Dim TXT2 As Shape

 For Each TXT2 In DESCRIP

    TXT2.TEXT.ConvertToParagraph
    TXT2.ObjectData("Name") = "DESCRIPTION-PARAGRAPH"

Next TXT2


DESCRIP.Combine



DESCRIP.SetSize drw, drh
DESCRIP.SetPosition drx, dry + drh


FINE2:



'TIDY GROUPS ADD ADD DESCRIPTION TO TEMPLATE GROUP
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Dim FINDTEMP As Shape
Set FINDTEMP = ActivePage.FindShape(name:="template")
Dim regrouped As Shape
Dim regrouper As ShapeRange
Set regrouper = FINDTEMP.UngroupAllEx
DESCRIP.CreateSelection
regrouper.Add ActiveSelection

Set regrouped = regrouper.Group
regrouped.ObjectData("Name") = "template"
regrouper.RemoveAll




Set objCmd = Nothing
Set objRS = Nothing
Set objConn = Nothing
ex:
End Function

Access Databases Wilcards

示例 1:

strSQL = "SELECT [id] FROM quotation WHERE reference Like '*" & jobno & "' AND version = " & ver

示例 2:

Const BaseSQL = "SELECT [id] FROM quotation WHERE reference Like '*@jobno' AND version = @ver"
strSQL = Replace(BaseSQL, "@jobno", jobno)
strSQL = Replace(strSQL, "@ver", ver)

Sequel Server Wildcards

示例 1:

strSQL = "SELECT [id] FROM quotation WHERE reference Like '%" & jobno & "' AND version = " & ver

示例 2:

Const BaseSQL = "SELECT [id] FROM quotation WHERE reference Like '%@jobno' AND version = @ver"
strSQL = Replace(BaseSQL, "@jobno", jobno)
strSQL = Replace(strSQL, "@ver", ver)