Solved

VB6 SQL Query

Posted on 2014-11-25
4
197 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
4 Comments
 
LVL 11

Accepted Solution

by:
jkpieterse earned 500 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 84
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

929 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now