Home > Microsoft .Net Development Tips > ARCHIVE: Tips & Tricks > PADQ solves SQL-Assignment troubles
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ARCHIVE: TIPS & TRICKS

PADQ solves SQL-Assignment troubles


Robert Berrios
09.26.2000
Rating: -4.00- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


PADQ Solves SQL-Assignment Troubles

Reader Robert Berrios sends in this tip that will help developers get their syntax straight when they are attempting to assign SQL statements to a string. You'll encounter Robert's problem any time that you're using the traditional way of doing this and encounter some data value that has unexpected or unusual punctuation.

When I first started using VB, one of my most burdensome tasks was assigning SQL statements to a string, particularly assigning statements that passed string values to SQL. I never got was it right the first time. It's especially difficult when you encounter values such as "O'Conner". I eliminated the problem when I discovered a function called PadQ.

Here's the old fashioned way:

Dim sSql As String
sSql = "Select lastname, firstname, ssn, homeadd, state, zip, tel,
hiredate "
sSql = sSql & "Where lastname = '" & sLastName & "'"
sSql = sSql & "And firstname = '" & sFirstName & "'"
sSql = sSql & "And ssn = '" & sSSN & "'"
sSql = sSql & "And homeadd = '" & sHomeAdd & "'"
sSql = sSql & "And state = '" & sState & "'"
sSql = sSql & "And zip = '" & sZip & "'"
sSql = sSql & "And tel = '" & sTel & "'"
sSql = sSql & "And hiredate = '" & sHireDate & "'"

Here's the new way with PadQ:

Dim sSql As String
sSql = "Select lastname, firstname, ssn, homeadd, state, zip, tel,
hiredate"
sSql = sSql & " Where lastname = " & PadQ(sLastName)
sSql = sSql & " And firstname = " & PadQ(sFirstName)
sSql = sSql & " And ssn = " & PadQ(sSSN)
sSql = sSql & " And homeadd = " & PadQ(sHomeAdd)
sSql = sSql & " And state = " & PadQ(sState)
sSql = sSql & " And zip = " & PadQ(sZip)
sSql = sSql & " And tel = " & PadQ(sTel)
sSql = sSql & " And hiredate = " & PadQ(sHireDate)

Public Function PadQ(ByVal sStringPassed As String) As String
Dim sNewString As String
Dim iPos As Integer
On Error GoTo PadQ_Error
sNewString = Trim$(sStringPassed)
iPos = InStr(1, sNewString, chr(39))
Do While iPos > 0
sNewString = Mid$(sNewString, 1, iPos) & chr(39) &
Mid$(sNewString, iPos + 1)
iPos = InStr(iPos + 2, sNewString, chr(39))
Loop
PadQ = chr(39) & sNewString & chr(39)
Exit Function
PadQ_Error:
'Error code.....................
PadQ = ""
End Function

If you use PadQ you won't need to remember where those quotes belong when you're passing string types to SQL. So it cuts down on your development time, especially when you have an "enormous" SQL statement stretching from here to Wisconsin. And it handles names with quote marks in them, such as O'Hara.

Robert, thanks for the great tip for our readers, and to show our appreciation, we're sending you a free searchVB.com denim shirt. Enjoy!

Rate this Tip
To rate tips, you must be a member of SearchWinDevelopment.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
ARCHIVE: Tips & Tricks
SearchVB wants your tips
Find out what VS.NET modules are installed
Retrieving icons from an EXE-file
Toggling Boolean values
VS.NET multiple tabbed windows
Set the value of a DataField in code behind
Easy grid design
Open using last Visual Studio.NET layout
Save development time by using inherited forms
Reusing code in VS.NET

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts