Solved

VB6 SQL Query

Posted on 2014-11-25
4
201 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 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

739 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