Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB6 SQL Query

Posted on 2014-11-25
4
Medium Priority
?
207 Views
Last Modified: 2014-11-26
I am writing VB6 for Email2DB script at my company and cannot seem to figure stuff out. I am new to VB6 I can do C# and its been a while since VB.net

Here is what I am looking to do.

I want to connect to my SQLEXPRESS server and run a query to see if something exists. Something like.

SELECT USERNAME FROM MFUSERS WHERE USERNAME= MSG.GetToAddr(i).

Then assign the result to a string

So Dim result as string = sql.response   (not sure how to code this either)

Then am having my vb script check if the MSG.GetToAddr = the sql.response. If so it does something. If not it does something else.

Here is my code so far.


' Outbound Mail Server
' This script fires before emails are sent via the Email2DB Mail Server.

' Use it to implement custom accept/reject rules, or change/add headers or message text.
' Set the property Email2DBAccept to True to accept the message.
' The current message can be accessed via the MSG object.

Sub Main()

    ' commands start here
 Email2DBAccept = Email2DBAccept
Dim numto As Long
numto = MSG.NumTo

If (numto > 0) Then

    For i = 0 To numto - 1
    'This checks to see if the email exists in the DB if it does then it does not relay it.
    'If it does not exists then it sends it normal and allows it to relay out.
    If MSG.GetToAddr(i) ="test@balls.com" Then
            Email2DBAccept =False
    Else
            Email2DBAccept=True


    End If


    Next
' This is the final end if below which has the starting of the If (numto) in the begining
End If
End Sub

Open in new window

0
Comment
Question by:desiredforsome
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 11

Accepted Solution

by:
jkpieterse earned 2000 total points
ID: 40466349
Typically you would use the ADO library to do this. There are gazillions examples out there aimed at Excel VBA which you can easily use in your VB6 project. The site http://erlandsendata.no/?cat=15 has connection strings for most databases.
0
 
LVL 85
ID: 40466358
What are you having trouble with? As jkpieterse said, you use ADO to connect to SQL Server - are you able to do that? Basically, it's something like:

Dim con As New ADODB.Connection
con.ConnectionString = "Your connection string"
con.Open
'/ if you are returning an integer value, perhaps:
Dim cnt As Integer
cnt - con.execute("SELECT Count(*) AS RecCount FROM SomeTable")
'/ if you need to open a Recordset:
Dim rst As ADODB.Recordset
rst.Open "SELECT * FROM SomeTable WHERE MyIDField=" & txSomeFieldOnMyForm, con

If Not (rst.EOF and rst.BOF) Then
  Msgbox rst("SomeField")
End If
0
 

Author Closing Comment

by:desiredforsome
ID: 40466975
Further research got me where i wanted.
0
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 40467262
Dim cn As New ADODB.Connection(<connection string>)
cn.open
if cn.execute("select 1 from users where username='" & myvariable & "'").eof then
  msgbox "user does not exist"
else
  msgbox "user exists"
end if
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question