Git Product home page Git Product logo

vba-summary's Introduction

VBA-Summary

1. SQL Connection

Reference: https://www.access-programmers.co.uk/forums/threads/how-to-make-an-ado-connection-public.167811/ https://stackoverflow.com/questions/34389720/connection-string-for-sql-server-2014-express-vba

'Tools > References > Check the checkbox in front of "Microsoft ActiveX Data Objects 2.5 Library"
Dim Conn1 As ADODB.Connection
Dim Cmd1 As ADODB.Command
Dim Param1 As ADODB.Parameter
Dim Rs1 As ADODB.Recordset


Private mcnn As ADODB.Connection
'https://www.access-programmers.co.uk/forums/threads/how-to-make-an-ado-connection-public.167811/
Function fGetConn() As ADODB.Connection
On Error Resume Next
 
    Dim Server_Name As String
    Dim Database_Name As String
    Dim User_ID As String
    Dim Password As String
    Dim fConnectionStr As String

    Server_Name = "172.16.254.99" ' Enter your server name here
    Database_Name = "DCSPOY1" ' Enter your database name here
    User_ID = "sa" ' enter your user ID here
    Password = "" ' Enter your password here

    fConnectionStr = "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
    ";Uid=" & User_ID & ";Pwd=" & Password & ";"
    
    
    
    If mcnn Is Nothing Or mcnn.Status = 0 Then
        Set mcnn = New ADODB.Connection
        mcnn.Open fConnectionStr 'Which returns whatever connection string you use
    End If
    Set fGetConn = mcnn
 
End Function
 
Sub CloseConn()
 
    mcnn.Close
    Set mcnn = Nothing
 
End Sub

How to call the function

Sub Button1_Click()
    Dim result_count As Integer
    Set Conn1 = fGetConn
    result_count = Record_Exist("2A", "01", "1", "2V34Q")
    MsgBox "Record: " & result_count
    Call CloseConn
    
End Sub

Function Record_Exist(LINE_ID As String, WINDER As String, END_NO As String, LOT_NO As String) As Integer

 Dim SELECT_STRING As String
 SELECT_STRING = "SELECT * FROM [DCSPOY1].[dbo].[Dynafil] WHERE LINE_ID='" & LINE_ID & "' AND WINDER='" & WINDER & "'"
 
 Rs1.Open SELECT_STRING, Conn1, adOpenStatic
 
 If Rs1.RecordCount < 0 Then ' Evaluate argument.
    Record_Exist = -1
  'Exit Function ' Exit to calling procedure.
 Else
  Record_Exist = Rs1.RecordCount
  
 End If
End Function

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.