SQL过滤WHERE字段

时间:2016-07-29 05:54:28

标签: sql vba coreldraw

这是我的第一篇文章,因为我已经设法应对所有现有的讨论,以解决任何问题或学习我所困扰的任何事情。然而,这个问题让我很好,真的很难过!

我工作的公司以工作号码保存个人工作,目前这是一个六位数的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?

以下完整代码(密码和数据库信息已消隐)

所有的crits和帮助欢迎, 感谢

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

1 个答案:

答案 0 :(得分:0)

Access Databases Wilcards

示例1:

app.js

示例2:

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

Sequel Server Wildcards

示例1:

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

示例2:

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